Ok I'm using a map.get() to stored select option values in Array
myArray = $("option:selected").map(function(){ return this.value }).get().join(" ");
I have some div's with ID names same as the select options value stored in Array
myArray = [ one two three ];
I want to apply [one two three] as class to a div.box and find the Div's with ID names same as myArray [div#one div#two div#three] and get all input:checked.
<div id="one"> inputs with :checked </div>
<div id="two"> inputs with :checked </div>
<div id="three"> inputs with :checked </div>
<div id="four"> inputs with :checked </div>
How to get :checked items from Div's with ID [ one two three ] only.
I'm not sure how to find the ID's that match each array element appreciate your help and thanks in advance :)
var selectedOptions = $(".select-field option:selected").map(function() {
return this.value
}).get().join(" ");
//add selected option value as CLASS to .box
$('.box').addClass(selectedOptions);
// check if selected option stored in array match any div ID and get it's checked values
$.each(selectedOptions, function(index, value) {
$('#' + value + ' input:checked').each(function() {
var checked = $(this);
});
});
//result
$("p.select").append(selectedOptions);
$("p.checked").append(checked);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="select-field">
<option value="one">option one</option>
<option value="two">option two</option>
<option value="three">option three</option>
</select>
<select class="select-field">
<option value="four">option four</option>
<option value="five">option five</option>
<option value="six">option six</option>
</select>
<div id="one">
<p>Option one</p>
<fieldset>
<input type="radio" name="div1" value="left">
<label for="left">Left</label>
<input type="radio" name="div1" value="right" checked>
<label for="right">Right</label>
</fieldset>
</div>
<div id="two">
<p>Option two</p>
<fieldset>
<input type="radio" name="div2" value="left" checked>
<label for="left">Left</label>
<input type="radio" name="div2" value="center">
<label for="center">Center</label>
</fieldset>
</div>
<div id="three">
<p>Option three</p>
<fieldset>
<input type="radio" name="div3" value="right">
<label for="right">Right</label>
<input type="radio" name="div3" value="center" checked>
<label for="center">Center</label>
</fieldset>
</div>
<div id="four">
<p>Option four</p>
<fieldset>
<input type="radio" name="div4" value="right">
<label for="right">Right</label>
<input type="radio" name="div4" value="center" checked>
<label for="center">Center</label>
</fieldset>
</div>
<div id="five">
<p>Option five</p>
<fieldset>
<input type="radio" name="div5" value="left">
<label for="left">Left</label>
<input type="radio" name="div5" value="center" checked>
<label for="center">Center</label>
</fieldset>
</div>
<br />
<h4>Results</h4>
<div style="border:2px solid" class="box">Add "option one" and "option two" value as Class to this div .box example class="one four"</div>
<p class="select">Selected Options are :</p>
<p class="checked">Checked Options are :</p>
id="select-field"twice...