change field value when select radio buttons

datisdesign picture datisdesign · Aug 24, 2009 · Viewed 32.7k times · Source

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.

Answer

mck89 picture mck89 · Aug 24, 2009

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" />