Issue
I am working on a select option where I do not know how can I get the text from the option base on the option value.
For example I would like to get the text "Danish" if the value is da,
The html of the option select is as below
<div id="language" class="dd-container" style="width: 333px;">
<div class="dd-select">
<input class="dd-selected-value" name="language" type="hidden" value="en">
<a class="dd-selected"><label class="dd-selected-text">English</label></a>
<span class="dd-pointer"></span>
</div>
<ul class="dd-options dd-click-off-close">
<li><a class="dd-option dd-option-selected">
<input class="dd-option-value" type="hidden" value="en">
<label class="dd-option-text">English</label></a>
</li>
<li><a class="dd-option">
<input class="dd-option-value" type="hidden" value="da">
<label class="dd-option-text">Danish</label></a>
</li>
<li><a class="dd-option">
<input class="dd-option-value" type="hidden" value="nl">
<label class="dd-option-text">Dutch</label></a>
</li>
</ul>
</div>
In my jquery, i am able to get the value with the below statment
var langname = $('li a.dd-option input.dd-option-value[value="da"]').val();
alert(langname);
But I am not able to get the text with this
var langname = $('li a.dd-option input.dd-option-value[value="da"]').text();
alert(langname);
Does anyone has a solution to this?
Solution
label is sibling of input element. You need to use correct selector to target the next label element:
$('li a.dd-option input.dd-option-value[value="da"]').next().text();
Answered By - Milind Anantwar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.