JQuery find () with: contains filter not working

I have a html list and I am trying to find a span element in the list with text equal to 'cat'

<div id= "target_language_tag_list">
<ul class="target-tag-list">
    <li><span id="tag_18" class="myTag">Γ©lΓ©phant</span></li>
    <li><span id="tag_31" class="myTag">box</span></li>
    <li><span id="tag_29" class="myTag">cat</span></li>
    <li><span id="tag_30" class="myTag">pig</span></li>
    <li><span id="tag_32" class="myTag">flower</span></li>
</ul>

      

I wrote the following js.coffee script, but I get a response

translatedTag = $('input#tag_translated_name').val()  # cat
targetList = $('#target-tag-list')
found = targetList.find("span:contains(" + translatedTagName + ")")
alert 'found: ' + found.attr('id')  # should be tag_29 but is undefined

      

I also try direct test, same undefined error

found = $("#tags.tab-pane.active #target-tag-list li span:contains('cat')")
alert 'found: ' + found.attr('id')  # should be tag_29 but is undefined

      

I'm guessing something is wrong with my filter usage ... but what is wrong?

+3


source to share


2 answers


Your UL doesn't have an ID, but a class.



Therefore, no element matches #target-tag-list

. Try looking for class with instead .target-tag-list

.

+4


source


Try this: http://jsfiddle.net/fpS4u/



translatedTag = $('input#tag_translated_name').val();
targetList = $('.target-tag-list');
found = targetList.find("span:contains("+translatedTag+")");
alert('found: ' + found.attr('id'));

      

+1


source







All Articles