Skip to content Skip to sidebar Skip to footer

Copy Value Of One Form Into The Hidden Field Of A Second Form

I have a page with multiple forms, however all forms need to take the value of a radio button in the products form. Below is a simplified version of my forms which are all on the

Solution 1:

ID attributes should be unique. If you don't need them, remove these. If you're using id=... for styling purposes, replace all occurences of id= by class=, and replace the sharp (#) in the CSS by a dot.

When a form is submitted, only elements with a name attribute are sent. This should work:

....
<script>functionfill(value) {
    var forms = document.forms;
    for (var i = 0; i < forms.length; i++) {
        if (forms[i].item_name) forms[i].item_name.value = value;
    }
}
</script></head><body>
...
<formname="products"method="post"action=""><inputonchange="fill(this.value)"name="prod_name"type="radio"value="Product 1"checked /><inputonchange="fill(this.value)"name="prod_name"type="radio"value="Product 2" /></form>
...

All form elements are accessible through their name at the form element. All forms are accessible (by name or by their index in the document) through the document.forms object.

When the radio selection changes, function fill() is called, passing this.value as an argument. From the context of the radio input elements, this.value points to the value of the radio element.

Then, we loop through all forms in the document. If item_name is an element in the form, the value is updated.

Post a Comment for "Copy Value Of One Form Into The Hidden Field Of A Second Form"