Milano Slesarik Milano Slesarik - 7 days ago 4
HTML Question

Uncaught TypeError: lab.find is not a function

I can't figure out why

JQuery
says that lab.find() is not a function. I'm looking for a text in this html:

<div class="btn-group col-md-4" data-toggle="buttons">

<label data-pk="4" id="languagebuttonselect1" class="btn prmr-btn-empty top-margin left-margin center-elements target_language_choice active" style="padding: 8px; color: black; width: 100%;">
<input type="checkbox" name="target_languages" value="4" autocomplete="off">

<img align="left" width="30px" style="margin-bottom: 0;" src="/static/img/flags/550px\gb.png">
<b class="left-margin" style="text-align: left">English</b>
</label>
</div>


And I want to get text (in this case "English") when a class of the tag (label) is
active
.

function refreshSummaryTaretLanguages(){
var actives = $('.target_language_choice.active');
var summ_ul_targ_langs = $('#summary_target_languages > ul');
summ_ul_targ_langs.empty();

$.each(actives,function (_,lab) {
summ_ul_targ_langs.append('<li>'+lab.find('b').text()+'</li>');
})
}


Unfortunately it can't find anything in such tag and I don't know what to do.

Answer

jQuery passes the $.each callback the elements of the array, so you're getting DOM nodes, not jQuery objects.

It would be simpler to just use the .each() method on the jQuery object you already have:

$(actives).each(function() {
    summ_ul_targ_langs.append('<li>'+$(this).find('b').text()+'</li>');
});

The .each() callback does basically the same thing as $.each(), and will also pass in the element. It also arranges for the callback to be invoked such that this references the element too. Wrapping this as $(this) gives you a jQuery object from which you can call .find().