how can i dispatch an event on my window ready? For example in my code i've got:
$("#info").click(function(){
// do stuff
});
I need to call this function the first time without have a click on #info, in the way // do stuff.
The HTMLElement.click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.
If you want native JS to trigger click event without clicking then use the element id and click() method of JavaScript.
The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.
bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to . bind() occurs.
You could use the .trigger()
method:
$('#info').trigger('click');
or simply:
$('#info').click();
which would be the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With