How to get ID of clicked element with jQuery
You just need to remove the hash from the beginning:
$('a.pagerlink').click(function() {
var id = $(this).attr('id').substring(1);
$container.cycle(id);
return false;
});
@Adam Just add a function using onClick="getId()"
function getId(){console.log(this.event.target.id)}
Your IDs are #1
, and cycle
just wants a number passed to it. You need to remove the #
before calling cycle
.
$('a.pagerlink').click(function() {
var id = $(this).attr('id');
$container.cycle(id.replace('#', ''));
return false;
});
Also, IDs shouldn't contain the #
character, it's invalid (numeric IDs are also invalid). I suggest changing the ID to something like pager_1
.
<a href="#" id="pager_1" class="pagerlink" >link</a>
$('a.pagerlink').click(function() {
var id = $(this).attr('id');
$container.cycle(id.replace('pager_', ''));
return false;
});