Suppose I have an anchor in my page like :<a href="#header" id="anchor">turn to header</a>
then I also set a button in my page <input type="button" value="triger anchor">
.What I want to do is that ,when I click the button ,it will triger the anchor's default click event ,as the page turn to "header".So I write $("input").click(function(){$("#anchor").trigger("click");});
But it seems doesn't work .So how can I achieve that?Thank you
Asked
Active
Viewed 1.4k times
3

hh54188
- 14,887
- 32
- 113
- 184
5 Answers
5
Try This, working for Me !!
$("#anchor").get(0).click();
This will call default click of the anchor.

Aurelio De Rosa
- 21,856
- 8
- 48
- 71

Usman Younas
- 1,323
- 15
- 21
4
Please see this answer.
Instead of trying to trigger the click
event, try setting the window.location
property:
window.location = '#header';
Or perhaps:
$("input").click(function() {
window.location = $('#anchor').attr('href');
});
-
Note that this will not send the referer for IE6, IE7 and I think IE8. – Peter Ajtai Sep 23 '11 at 20:32
0
jQuery does not have a way to trigger the native DOM event directly.
To do that, you can use the plain old standard DOM methods:
var evt = document.createEvent("Event");
evt.initEvent("click", true, true);
$('#anchor').get(0).dispatchEvent(evt);
For support of IE8 or earlier, you need to use the IE-specific event methods createEventObject
and fireEvent
.

chowey
- 9,138
- 6
- 54
- 84
0
You can Also use to trigger click/any events
$('#bycategory').click(function(){
alert('triggered');
});
$('#bycategory').triggerHandler("click");

Elamurugan
- 3,204
- 13
- 59
- 104
-1
You have a spelling error. Please use:
{$("#anchor").trigger("click");});

Rob Hruska
- 118,520
- 32
- 167
- 192

Andrei Andrushkevich
- 9,905
- 4
- 31
- 42
-
……I'm sorry ,it just a write mistake .In my project it is"trigger",and doesn;t work…… – hh54188 Dec 19 '10 at 08:04
-
ok, try to add $("#anchor").bind('click', function(){alert('click')}); it is help you to check that event is happend – Andrei Andrushkevich Dec 19 '10 at 08:13
-
also add attrib. id for button and use it in your jQuery's query – Andrei Andrushkevich Dec 19 '10 at 08:15
-