chap
chap

Reputation: 1869

How to change the value of hidden form data when a radio button is selected?

What I want to do is when a user selects one of the radio button options its value will be passed to the hidden input's value. I can then use this data to display a particular form. How would I do this using Jquery/Javascript?

"<form id=""categoryedit""action=""category.asp"" method=""post"">" &_
                   "<input type=""hidden"" name=""stage"" value ="""">"&_
                   "<label for=""addcategory"">Add Category</label>"&_
                   "<input id=""addcategory"" type=""radio"" name=""addcategory"" value=""2""><br>" &_
                   "<label for=""deletecategory"">Delete Category</label>"&_
                   "<input id=""deletecategory"" type=""radio"" name=""deletecategory"" value=""4""><br>" &_
                   "<label for=""editcategory"">Edit Category</label>"&_
                   "<input id=""editcategory"" type=""radio"" name=""editcategory"" value=""6""><br>" &_
                   "<input class=""button"" type=""submit"" value=""Select"">" &_
                   "</form>"

Upvotes: 1

Views: 953

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

$('#categoryedit input[type="radio"]').click(function(){
    $(this.form).find('input[name="stage"]').val(this.value)
})

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

Firstly, all your radio buttons should have the same name so that they are grouped. Then this code should work for you:

$('#categoryedit input[type="radio"]').change(function() {
   $('#categoryedit input[type="hidden"]').val($(this).val());
});

Example fiddle

Note in the example I change the hidden field so that it's visible, so you can see the code working.

Upvotes: 2

Related Questions