get value of an attribute of the clicked element

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)