5

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

kobe
  • 15,671
  • 15
  • 64
  • 91
GoodSp33d
  • 6,252
  • 4
  • 35
  • 67

3 Answers3

10

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

$("#anc").trigger('click');
pthurlow
  • 1,091
  • 7
  • 11
3

you need to use # for id

$("#anc").click(function() {
    return false // this will kill the default behaviour and your page won't jump
});
kobe
  • 15,671
  • 15
  • 64
  • 91
0

when using an id you can use ('#idname').trigger('click') , when using a class you can use ('.classname').trigger)('click')

Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193