Search code examples
javascripthtmlradio-buttonhidden-field

change field value when select radio buttons


I want to change the value of hidden input field when radio buttons selected :

    <input type="radio" name="r1" value="10" />10
    <br/>
    <input type="radio" name="r1" value="45" />45
    <br/>
    <input type="hidden" name="sum" value="" />

for example when user click on one the buttons the value of hidden field change to that value.


Solution

  • Use the onClick property:

    <input type="radio" name="r1" value="10" onClick="document.getElementById('hidfield').value=this.value"/>10
        <br/>
        <input type="radio" name="r1" value="45" onClick="document.getElementById('hidfield').value=this.value"/>
        45
        <br/>
        <input type="hidden" name="sum" value="" id="hidfield" />