Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 907
datisdesign Avatar asked Aug 24 '09 13:08

datisdesign


3 Answers

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" />
like image 106
mck89 Avatar answered Sep 22 '22 20:09

mck89


You can try for example

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

jQuery("input[id^='radio']").click(function() {
    jQuery("input[name='sum']").val(jQuery(this).val());
}

So then when user click on each radio we handle it by various id with same start.

like image 30
Alexey Ogarkov Avatar answered Sep 20 '22 20:09

Alexey Ogarkov


Using jQuery it would be:

$(":radio").click(function () {
    var inputValue = $this.val();
    $(":hidden[name='sum']").val() = inputValue;
$(":hidden[name='sum']").name() = "lalala";
    });

I've not double checked that code so it might need a little tweaking.

like image 22
Dorjan Avatar answered Sep 22 '22 20:09

Dorjan