Eyal Eyal - 4 months ago 22
jQuery Question

Get the next span with class

I have the following Html structure for every

<select>
object in my page:

<div class="clsPropertyDiv clsPropertyId_16 ">
<span class="inputTitle">You can select multiple items</span><br>
<select id="select_16" multiple="multiple" style="display: none;">
<option class="" value="2699" id="opt_2699">Rusian </option>
<option class="clsOptionSelected" value="2700" id="opt_2700">English </option> // take this text
<option class="" value="2702" id="opt_2702">Hebrew</option>
</select>

<div class="ms-parent multipleSelection" multiple="multiple">
<button type="button" class="ms-choice">
<span class="spnMulti"></span> // insert here.
<div class=""></div>
</button>
</div>
</div>


I need to get the selected text and insert it to:
<span class="spnMulti"></span>


This is what I was trying to do:

$('.selectOptions option').each(function() {
if ($(this).hasClass('clsOptionSelected') {
var selectedText = $(this).text();
$(this).next('.spnMulti').text("selectedText ");

}
});

Answer

The .spnMulti don't are childen of selectOptions. You need call the parent div and after find by spnMulti.

Example:

$(this).closest('.clsPropertyDiv').find('.spnMulti').text(selectedText);

and add class to select "selectOptions", in selector, you use: "$('.selectOptions option')" and not have a .selectOptions class in select.

Comments