Skip to content Skip to sidebar Skip to footer

Get Value Of An Attribute Of The Clicked Element

  • english
  • francais
  • italiano
when

Solution 1:

Original answer - 2011

$('li').click(function () {
    alert($(this).data('val'));
});

See DEMO.

Update - 2017

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.


Solution 2:

This should do the job:

$(document).ready(function() {

  $('#langs li').click(function() {
    alert($(this).attr('data-val'));
  });
});

Have a look at the Docs


Solution 3:

$('li').click(function() {
    alert($(this).attr('data-val'));
});

Post a Comment for "Get Value Of An Attribute Of The Clicked Element"