Similar to this question here, I'm looking to get an element based on it's data-id.
<a href="#" class="whatever" data-item-id="stand-out">...</a>
I'm looking to take action on the a tag. It has a shared class amongst many other elements and has no base id, just a data-item-id.
Can the element be found by it's data-item-id or can I only find out what the data-item-id is?
Use the querySelector method to get an element by data attribute, e.g. document. querySelector('[data-id="box1"]') . The querySelector method returns the first element that matches the provided selector or null if no element matches the selector in the document. Here is the HTML for the examples in this article.
ready(function() { $('#list li'). click(function() { alert($(this). attr("id")); alert($(this). text()); }); });
To retrieve a data-* attribute value as an unconverted string, use the attr() method. Since jQuery 1.6, dashes in data-* attribute names have been processed in alignment with the HTML dataset API. $( "div" ).
id is not a valid jquery function. You need to use the . attr() function to access attributes an element possesses.
$('[data-item-id="stand-out"]')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With