0
<div data-letter='1'>1</div>
<div data-letter='2'>2</div>
<div data-letter='3'>3</div>

Is any way to select element by data-letter? ex. I want to select data-letter "2"

user2178521
  • 833
  • 1
  • 14
  • 26
  • [attribute equals selector](http://api.jquery.com/attribute-equals-selector/) – Arun P Johny Aug 28 '14 at 09:01
  • [jQuery how to find an element based on a data-attribute value?](http://stackoverflow.com/questions/4191386/jquery-how-to-find-an-element-based-on-a-data-attribute-value) – Arun P Johny Aug 28 '14 at 09:02

2 Answers2

2

Use attribute selector

$("[data-letter='2']").html()
Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
1
$("div").each(function(){
  if($(this).attr("data-letter")=="2")
  {
    alert($(this).html());
  }
});

OR

If there are many div's with data-letter = 2 ,then do as :

$("div[data-letter='2']").each(function(){
   alert($(this).html());
});

if there is only one div with data-letter = 2 ,then do as :

$("div[data-letter='2']").html()
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69