Setting RadioButton using jQuery should be simple. But, there is a small trivia about it. Consider a simple radiobutton group called Options which has values of Yes and No.

If you want to set the initial value of Option to No, how would you do it? You would probably use the val function as below:

But, the above code does not work. To avoid this problem, I use to write code using the click method as follows:

Though the above code works, it is not elegant. The right way to set the RadioButton value is to pass an array of value(s) to the val() function as follows:

The above code works well, and according to the jQuery API documentation, the preferred way to set RadioButton values.

Set RadioButton value using jQuery
Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *