2

I have created group of radio buttons using <c:forEach> tag. I have given ids also.

On click of any radio button i do a ajax call. Once i come back to that page i want to re select that radio button.

For this i assign back selected radio button ID to a hidden text box.

My question is how to make radio button selected with this hidden text box value.

I tried like

$('input[id="$("#myHiddenTextBoxId").val()"]').attr('checked', true);

But it is not working. Here myHiddenTextBoxId hold value of previously selected radio button.

3 Answers 3

3

You where sending it all as a string - you need to split it out

$(document).ready(function() {
   $('input[id="' + $("#myHiddenTextBoxId").val() + '"]').prop('checked',true);
});

prop() should be used as of jQuery 1.6 -> http://api.jquery.com/prop/

Note Also ensure that your code is enclosed within the $(document).ready() function to ensure that the DOM is ready before trying to manipulate it.

1
$('#myHiddenTextBoxId').prop('checked', true);
1
  • @DidierG.: You may be right, but I did not see the HTML that OP referred to.
    – Tadeck
    Jan 6, 2012 at 10:38
1

Try this:

$('input[id="' + $("#myHiddenTextBoxId").val() + '"]').prop('checked', true);

You cannot have a jquery function call ($("#myHiddenTextBoxId").val()) in the selector string.

Working example here.


Note:

Use .attr() this way: .attr('checked', 'checked')
Although, for special attributes like checked or disabled, use .prop('checked', true) (jquery 1.6+)

3
  • HI @Didier G I tried your solution but it did not work. It gives java script error. I tried with .prop()
    – tomK
    Jan 6, 2012 at 10:29
  • Well, my solution works (jsfiddle). Is the ID saved in your hidden field correct ? Jan 6, 2012 at 10:35
  • Does your radio button exists in the DOM when you try to re-select it ? Jan 6, 2012 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.