How to use jquery trigger anchor default click event?
Suppose I have a binding on my page, for example:, <a href="#header" id="anchor">turn to header</a>then I also set a button on my page <input type="button" value="triger anchor">. What I want to do is that when I click the button, it will be the default trigger for the event binding, as the page turns into a "title". So I'm writing $("input").click(function(){$("#anchor").trigger("click");});But it doesn't seem to work. So how can I achieve this? thank
See this answer .
Instead of trying to fire the event, clicktry setting the property window.location:
window.location = '#header';
Or perhaps:
$("input").click(function() {
window.location = $('#anchor').attr('href');
});
jQuery is not able to directly trigger its own DOM event.
You can use simple standard DOM methods for this:
var evt = document.createEvent("Event");
evt.initEvent("click", true, true);
$('#anchor').get(0).dispatchEvent(evt);
To support IE8 or earlier, you need to use event methods specific to IE createEventObjectand fireEvent.