最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select element by element's content - Stack Overflow

programmeradmin1浏览0评论

I have list:

<ul class='mates'>
   <li class='m' id='1'>Jakub</li>
   <li class='f' id='2'>Vinnie</li>
   <li class='m' id='3'>David</li>
</ul>

How can I select 'li' tags "ONE BY ONE" to be checked if their content (between 'li' tags) is equal to 'xyz'.

element = document.getElementsByClassName('.mates').firstChield.innerHTML;
do {
    if(){
        //do something
    }
}while (element = element.nextSibling);

but I'm not getting even far enough to select firstChild. This error is showing in console: "Cannot read property 'innerHTML' of undefined". This needs to be done in plain JavaScript. Any ideas?

I have list:

<ul class='mates'>
   <li class='m' id='1'>Jakub</li>
   <li class='f' id='2'>Vinnie</li>
   <li class='m' id='3'>David</li>
</ul>

How can I select 'li' tags "ONE BY ONE" to be checked if their content (between 'li' tags) is equal to 'xyz'.

element = document.getElementsByClassName('.mates').firstChield.innerHTML;
do {
    if(){
        //do something
    }
}while (element = element.nextSibling);

but I'm not getting even far enough to select firstChild. This error is showing in console: "Cannot read property 'innerHTML' of undefined". This needs to be done in plain JavaScript. Any ideas?

Share Improve this question asked May 23, 2013 at 14:28 Jakub ZakJakub Zak 1,2326 gold badges33 silver badges53 bronze badges 2
  • Why can't you use jQuery? – Geeky Guy Commented May 23, 2013 at 14:29
  • Assessment. It have to be plain JavaScript. – Jakub Zak Commented May 23, 2013 at 14:29
Add a ment  | 

4 Answers 4

Reset to default 3
<ul class='mates'>
   <li class='m' id='1'>Jakub</li>
   <li class='f' id='2'>Vinnie</li>
   <li class='m' id='3'>David</li>
</ul>
<script>
var mates = document.getElementsByClassName('mates')[0];
for (var i=0; i< mates.childNodes.length; i++){
    if(mates.children[i].innerHTML == 'Vinnie') alert("Got you! ID "+mates.children[i].id)
}
</script>

Drop the dot in the parameter. Like this:

element = document.getElementsByClassName('mates').firstChild.innerHTML;

The dot is not a part of the name of the class.

EDIT also notice that the question originally had a typo in firstChild.

Your element variable is not an element (its value is probably undefined). It should work if you use it like this:

var element = document.getElementsByClassName('mates')[0].firstChild;
do {
    if(element.innerHTML == 'foo'){
        //do something
    }
} while (element = element.nextSibling);

The code above fixes:

  • The class name as pointed out by @Renan
  • The typo in .firstChild

Also note that getElementsByClassName returns a list of elements, so you have to grab the first one in the list (index 0) to reach your <ul>.

Finally, keep in mind that you'll be looping over all children of the <ul>, including empty text nodes (see a demonstration at http://jsfiddle/58ZZF/). This can be avoided if you use firstElementChild and nextElementSibling, but I'm not sure if there are cross browsers issues with those properties (MDN only says it's Firefox 3.5+).

Few mistakes

  1. Class name to getElementsByClassName should not have .
  2. Spelling mistake in firstChild
  3. getElementsByClassName returns an array, not a dom reference
  4. When using nextSibling it could return text nodes also, you need to check the nodeType to make sure the element is a element node(nodeTye = 1), also you can check the tagName == 'LI'

Try

var element = document.getElementsByClassName('mates')[0].firstChild;
do {
    if(element.nodeType == 1){
        console.log(element.textContent)
    }
}while (element = element.nextSibling);

Demo: Fiddle

发布评论

评论列表(0)

  1. 暂无评论