I am trying to parse through all elements in a single radio button (or check box).
In my code, $(this) refers to a specific radio button or checkbox…
What I want is to create a function using ‘each’ which parses through all values of a radio or check box..
The code I am trying to create is shown below–
...SOME CODE....
...SOME CODE....
else if (($(this).attr('type')=="radio")||($(this).attr('type')=="checkbox"))
{
$(this).<WHAT SHOULD BE THE CODE HERE>.each(function(){
alert( " Option value=" + $(this).val() );
});
}
What should be the code at the place indicated above, so that I can parse through all elements of a radio button/check box. Also, I want to obtain both text values and assigned values, I suppose this can be done using text() and val() respectively?
EXAMPLE OF A CHECK BOX —
<input type="checkbox" name="color" value="red1">
Red<br>
<input type="checkbox" name="color" value="yellow1">
Yellow<br>
<input type="checkbox" name="color" value="blue1">
Blue<br>
<input type="checkbox" name="color" value="orange1">
Orange<br>
<input type="checkbox" name="color" value="green1">
Green<br>
<input type="checkbox" name="color" value="purple1">
Purple<br>
As can be seen from above example, for each element of the checkbox, using val() should fetch “red1”, “yellow1”, “blue1” and so on…while using text() should fetch “Red” “Yellow” “Blue” and so on…
1 Answer