How to get text() by attr() in jquery -


here's 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 have text() value attr() have data-g set.

right code tried don't work

$('.question .button a').click(function(){       var atext = $(this).parents('.question').find('data-g').text();   return atext; }); 

i've tried 1 text inside .question div (2002,2010,1985,2013)

$('.question .button a').click(function(){       var atext = $(this).parents('.question').text();   return atext; }); 

so how can have result of div data-g attribute , 2010 text.

thanks

$('.question .button a').click(function() {     var atext = $(this).closest(".question").find("[data-g]").text();                       // check `true` use ("[data-g='true']")     console.log(atext); });​ 

demo: http://jsfiddle.net/2ny7j/


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

javascript - Preserving URL fragment through CAS sign-on -

asp.net mvc - Implementing normal user/pass, Twitter & Facebook auth -