Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger OnClick in anchor tag

How can i use trigger() in jquery to emulate onclick attribute of a anchor tag:

<a id="anc" href="#" onclick="someFunction()"> some text </a>

here if i use

$("anc").trigger('click');

its not working

like image 580
GoodSp33d Avatar asked May 21 '11 06:05

GoodSp33d


2 Answers

you need to correctly reference the id "anc" in the jQuery selector with a #:

$("#anc").trigger('click');
like image 172
pthurlow Avatar answered Oct 19 '22 23:10

pthurlow


you need to use # for id

$("#anc").click(function() {
    return false // this will kill the default behaviour and your page won't jump
});
like image 32
kobe Avatar answered Oct 20 '22 00:10

kobe