0

How can I check if my list .sidebar contains data-id="1".

<ul class="sidebar">
  <li data-id="1">Option 1</li>
  <li data-id="2"> Option 2</li>
  <li data-id="3"> Option 3</li>
</ul>
5
  • $('.sidebar').each(function({})); Commented Aug 15, 2016 at 20:58
  • 2
    That isn't valid HTML. I doubt it's producing what you think it's producing. Commented Aug 15, 2016 at 20:58
  • update ur question with proper html and details Commented Aug 15, 2016 at 20:58
  • @JAG u sure, don't see any edit? Commented Aug 15, 2016 at 20:59
  • Assuming it was valid, as in <li data-id="1"></li> you could do $('.sidebar li[data-id="1"]').length Commented Aug 15, 2016 at 21:01

2 Answers 2

1

Select all LI's with a data-id attribute with the value 1, and check the length of the collection to see if that element exists

var exists = $('.sidebar li[data-id="1"]').le‌​ngth !== 0;
Sign up to request clarification or add additional context in comments.

Comments

1

Use the attribute selector:

var hasIt = $('ul.sidebar[data-id="1"]').size() > 0;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.