Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
How can I check if my list .sidebar contains data-id="1".
.sidebar
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>
html
<li data-id="1"></li>
$('.sidebar li[data-id="1"]').length
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
data-id
1
var exists = $('.sidebar li[data-id="1"]').length !== 0;
Add a comment
Use the attribute selector:
var hasIt = $('ul.sidebar[data-id="1"]').size() > 0;
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
htmland details<li data-id="1"></li>you could do$('.sidebar li[data-id="1"]').length