Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery get an element by its data-id

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?

like image 905
MacSalty Avatar asked Nov 08 '13 18:11

MacSalty


People also ask

How do you select an element with a data id?

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.

How can get li id value in jQuery?

ready(function() { $('#list li'). click(function() { alert($(this). attr("id")); alert($(this). text()); }); });

How get data attribute in jQuery?

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" ).

Can I use id in jQuery?

id is not a valid jquery function. You need to use the . attr() function to access attributes an element possesses.


1 Answers

$('[data-item-id="stand-out"]')

like image 118
kumarharsh Avatar answered Oct 09 '22 04:10

kumarharsh