Reputation: 71188
<ul id='langs'>
<li data-val='en'>english</li>
<li data-val='fr'>francais</li>
<li data-val='it'>italiano</li>
</ul>
when the user clicks on any of these <li>
I want to alert()
it's data-val attribute value
anybody knows how?
Upvotes: 33
Views: 62873
Reputation: 111278
$('li').click(function () {
alert($(this).data('val'));
});
See DEMO.
Keep in mind that if you want to use the ES6 arrow function syntax, you cannot use this
and you need to use e.currentTarget
instead, where e
is the event object passed as the first parameter to the event handler:
$('li').click(e => alert($(e.currentTarget).data('val')));
See DEMO.
Upvotes: 44
Reputation: 14625
This should do the job:
$(document).ready(function() {
$('#langs li').click(function() {
alert($(this).attr('data-val'));
});
});
Have a look at the Docs
Upvotes: 21
Reputation: 1038710
$('li').click(function() {
alert($(this).attr('data-val'));
});
Upvotes: 5