Shiva Roy
Shiva Roy

Reputation: 299

How to get an element by its data-itemindex?

Suppose I want to get innerHTML of below <li> by its data-itemindex. i even don't know it is possible or not.

<li id="li:90" class="liGrid" data-itemindex="3" data-itemid="li:90" >
winoria</li>

i tried

alert($("li").find("[data-itemindex=3]").html());
alert($("li[data-itemindex='3']").text());

from How to select elements with jQuery that have a certain value in a data attribute array but doesnt help me.

Upvotes: 0

Views: 353

Answers (3)

Bradley
Bradley

Reputation: 490

Try This:

var data = $('li').data('itemindex', 3).text();
alert(data);

Upvotes: 0

Ryan Brodie
Ryan Brodie

Reputation: 6620

Use the CSS tag selector to locate the matching element/s within the DOM:

$("[data-itemindex=3]")

You can even do some more advanced selectors using a similar syntax:

[title~=flower] /* Selects all elements with a title attribute containing the word "flower" */
[lang|=en] /* Selects all elements with a lang attribute value starting with "en" */
a[src$=".pdf"] /* Selects every <a> element whose src attribute value ends with ".pdf" */
a[src^="https"] /* Selects every <a> element whose src attribute value begins with "https" */

Full documentation.

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82241

You can use:

 $('li[data-itemindex="3"]').text();

or

 $('li[data-itemindex="3"]').html()

Working Demo

Upvotes: 0

Related Questions