maxum maxum - 3 months ago 12
jQuery Question

get ID of li when swiped

I have a list as follows.

<ul id="hook_list">
<li id="a1">one</li>
<li id="a2">two</li>
<li id="a3">three</li>
<li id="a4">four</li>
</ul>


I am using
hammer.js
to detect swipe events ..

var myElement = document.getElementById('hook_list');
var mc = new Hammer(myElement);
mc.on("swipe", function(ev) {
alert('Detect swipe');
});


It detects the swipe no problem but I am not sure how to pull the
id
from the swiped
li
. I have tried
$(this).attr('id')
as I have jquery on the same page but I just get
undefined
.

How do I get the
id
from the swiped
li
?

Answer

Since you are binding the swipe event to the ul, you can't use this to get the id of the ul

However, since you have the reference to the event object in ev you can use the ev and use ev.target.id