turn to ...">

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

+3
source share
5 answers

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');
});
+4
source

Try this while working for me !!

$("#anchor").get(0).click();

.

+5

You can also use to trigger click / any events

$('#bycategory').click(function(){
  alert('triggered');
});
$('#bycategory').triggerHandler("click");
0
source

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.

0
source

You have a spelling mistake. Please, use:

{$("#anchor").trigger("click");});
-1
source

Source: https://habr.com/ru/post/1781088/


All Articles