How to get text() by attr() in jquery
jquery
Solution
$('.question .button a').click(function() {
var aText = $(this).closest(".question").find("[data-g]").text();
// to check for `true` use ("[data-g='true']")
console.log(aText);
});
DEMO: http://jsfiddle.net/2ny7j/
Problem
Here's the questions ``` <div class="question"> <div class="button" data-q="1" data-a="1"><a href="javascript:void();">2002</a></div> <div class="button" data-q="1" data-a="2" data-g="true"><a href="javascript:void();">2010</a></div> <div class="button" data-q="1" data-a="3"><a href="javascript:void();">1985</a></div> <div class="button" data-q="1" data-a="4"><a href="javascript:void();">2013</a></div> </div> ``` I would like to have the text() value for the attr() that have data-g set. Right now the code I tried don't work ``` $('.question .button a').click(function(){ var aText = $(this).parents('.question').find('data-g').text(); return aText; }); ``` I've tried this one but I get all text inside the .question DIV (2002,2010,1985,2013) ``` $('.question .button a').click(function(){ var aText = $(this).parents('.question').text(); return aText; }); ``` So how can I have only the result of the DIV with data-g attribute and get only the 2010 as text. Thanks