how to identify li item using jquery -
i've list
<ul> <li><a href="?page=4">one</li> //no 4 <li><a href="?page=7">two</li> //no 7 <li><a href="?page=14">three</li> //no 14 <li><a href="?page=72">four</li> //no 72 <li><a href="?page=201">five</li> //no 201 </ul>
now want use ajax load pages instead of normal page load. how detect link user clicked using jquery. like
$('id of element clicked').click(function()({ // load page using .ajax() });
in jquery code above, how id of element clicked
??
you fetch id href
attribute of clicked anchor using regex:
$('ul a').click(function() { var id = this.href.match(/([0-9]+)$/)[1]; // load page using .ajax() });
but better approach imho instead of using regex parse url use html5 data-*
attributes:
<ul> <li><a href="?page=4" data-id="4">one</li> <li><a href="?page=7" data-id="7">two</li> <li><a href="?page=14" data-id="14">three</li> <li><a href="?page=72" data-id="72">four</li> <li><a href="?page=201" data-id="201">five</li> </ul>
and then:
$('ul a').click(function() { var id = $(this).data('id'); // load page using .ajax() });
some other answers suggesting using id
attribute adding id="4"
anchor invalid markup according specification id of dom element cannot start number.
Comments
Post a Comment