In my code i am using a group of radio button having same id and name for all of the radio buttons.. but its value is different…
I have that value of the radio button that i want to be selected.. how can select that radio button using the value i have.
consider an example..
<input name="newColorCode" id="newColorCode" type="radio" value="color_code_LightViolet"/>
<input name="newColorCode" id="newColorCode" type="radio" value="color_code_red"/>
<input name="newColorCode" id="newColorCode" type="radio" value="color_code_green"/>
i have the value color_code_green.. how can i make the corresponding radio button selected using jquery????
i tried this
$('input:radio[value=color_code_green]').checked=true;
but its not working
First, ids must be unique in the whole DOM. So having 3 radios with
id="newColorCode"is wrong. So start by removing those id attributes or provide unique ones for each button. Then to answer your question:And here’s a live demo.
The reason your code doesn’t work is because the
$(..)function returns an array of jQuery wrapped DOM elements for which thecheckedproperty which you are trying to set is simply not defined. So you could use the.attr()function in order to do this to all elements returned by your selector.