the situation I'm struggling with is that there are more forms on the page that looks like this (the hiddenId is different in each form):
<form method="post">
<input type="hidden" name="hiddenId" value="111222">
<input type="submit" value="Proceed">
</form>
<form method="post">
<input type="hidden" name="hiddenId" value="111333">
<input type="submit" value="Proceed">
</form>
How do I submit with javascript (I don't mind using jQuery) a specific form that includes hiddenId of a wished value? Thank you for your help!
The <input type="hidden"> defines a hidden input field. A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.
It is actually possible to "submit" a "form" without form-tags. what you need is an ajax-function, e.g. with jquery, which will get the values by id. But there is no reason not to use a form tho. you should use form tag.
duplicate IDs are illegal in html. what your code is doing is the proper behavior - dealing with the FIRST id found in the document, and ignoring the rest (the dupes). read the docs for getElementById().
The id attribute specifies a unique id for an HTML element. The value of the id attribute must be unique within the HTML document. The id attribute is used to point to a specific style declaration in a style sheet. It is also used by JavaScript to access and manipulate the element with the specific id.
Something along these lines should get you started:
var inputs = document.getElementsByTagName("input");
for(var i = 0; i < inputs.length; i++) {
if(inputs[i].type === "hidden" && inputs[i].value === "111333") {
inputs[i].form.submit();
}
}
If you can use jQuery:
$("input[type='hidden'][value='something']").closest("form").submit();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With