Explanation:
- Retrieve the Form by ID: Use
document.getElementById(formId)
to select the specific form.
- Query Hidden Input: Use
form.querySelector('input[name="UserId"]')
to find the hidden field within that form.
- Get the Value: Access the value of the hidden field using
hiddenInput.value
.
- Dynamic Form IDs: Pass the form ID to the
getUserId
function dynamically (e.g., form1
, form2
).
function getUserId(formId) {
// Get the form element by ID
var form = document.getElementById(formId);
// Find the hidden input with name="UserId" inside the form
var hiddenInput = form.querySelector('input[name="UserId"]');
// Get the value of the hidden input
if (hiddenInput) {
var userId = hiddenInput.value;
console.log("UserId:", userId);
// Do something with the UserId
alert("UserId: " + userId);
} else {
console.error("No hidden input with name='UserId' found in form with ID:", formId);
}
}