user866933 user866933 - 3 months ago 9
jQuery Question

How to get id of select box after a known text inside label tag?

I have a div as shown below. I know there will be one text "User Type" within a label tag. There may be more than one text "User Type", but only one inside label tag. I need to get the id of tag select coming after this label. In this case it will be user_type_22. How i can get it using jquery?

<div class="form-group">

<label class="control-label" for="user_type_22">User Type</label>
<div class="control">
<select id="user_type_22" class="form-control" name="whateverbeit">
<option>1</option>
<option>2</option>
</select>
</div>

</div>

Answer

you can do it without a loop :

id = $("label:contains('User Type')").next('div').find('select').attr('id');
alert(id);

https://jsfiddle.net/z4qzqkhf/