I need to check if the value of the radio:checked or the user input is equal to a radio group. There are several input radio groups. I store each radio group in array.
I am trying to get the user input and checking if it exist in array of the radio group please help.
var userInput = $(this).val();
radioGroupOne = $(value).find('input[name="optionOne"]').map(function() {
return this.value
}).get().join(", "),
radioGroupTwo = $(value).find('input[name="optionTwo"]').map(function() {
return this.value
}).get().join(", ");
if ($.inArray(userInput, radioGroupOne) >= 0) {
alert(userInput + 'is in group one');
}
if ($.inArray(userInput, radioGroupTwo) >= 0) {
alert(userInput + 'is in group Two');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<br>
<label class="title">Group one</label>
<fieldset class="options-block">
<input type="radio" name="optionOne" value="red">
<label>Red</label>
<input type="radio" name="optionOne" value="blue">
<label>Blue</label>
<input type="radio" name="optionOne" value="green">
<label>Green</label>
</fieldset>
<br>
<label class="title">Group two</label>
<fieldset class="options-block">
<input type="radio" name="optionTwo" value="banana">
<label>banana</label>
<input type="radio" name="optionTwo" value="grape">
<label>grape</label>
<input type="radio" name="optionTwo" value="lemon">
<label>lemon</label>
</fieldset>
<br>