user4303218
user4303218

Reputation:

Select Element on the basis of attribute

<ul>
  <li year='2014'> </li>  
  <li year='2014'> </li>
  <li year='2014'> </li>
  <li year='2015'> </li>
</ul>

Using Javascript how can I select those li item whose year attribute is set to 2014. I tried the following, but it did not work:

document.getElementsByTagName("li[value=='2015']);

Upvotes: 0

Views: 97

Answers (1)

techfoobar
techfoobar

Reputation: 66663

You can use document.querySelectorAll() for this:

In this case, the usage should be document.querySelectorAll('li[year="2014"]')

Demo: http://jsfiddle.net/njvwxsxd/

Docs: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll

Upvotes: 3

Related Questions