Skip to content Skip to sidebar Skip to footer

Reading From Input Tag Outside Form Element

I have a web page with an input field (a calendar to select a date from). The problem is that I need to read that input field from 2 different form elements that each have their ow

Solution 1:

Just give it unique ID and use document.getElementById like this anywhere you want:

var myValue = document.getElementById("myInputID").value;

This way it doesn't matter where the input is located, it can even be outiside of any form.

Edit: To read the value upon submit, first add the onsubmit part to the relevant form:

<formaction="....."onsubmit="ReadValue(this);">

And now have this in your page:

<scripttype="text/javascript">functionReadValue(oForm) {
   var myValue = document.getElementById("myInputID").value;
   alert("value is: " + myValue);
}
</script>

This will show the value as alert dialog.. if you want to populate hidden form field with the value, have such code instead:

functionReadValue(oForm) {
   var myValue = document.getElementById("myInputID").value;
   oForm.elements["myHiddenFieldId"].value = myValue;
}

Hope it's clear enough! :)

Post a Comment for "Reading From Input Tag Outside Form Element"