Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does jquery .click() work on href when adding [0]?

So I had a problem where I needed a page to refresh but I couldn't use the .click() method on it because it does not work on href. I couldnt use window.location because I was trying to render a subtab which can be directly accessed due to security reasons. Then I stumbled upon this post:

https://stackoverflow.com/a/12801548/1864552

His solution works but I was hoping someone could give a good explanation on why this works.

like image 864
JS noob Avatar asked May 26 '26 02:05

JS noob


2 Answers

jQuery click function triggers the event handlers that were bound with jQuery and simulates an event to try to trigger other click related handlers, but can't exactly reproduce the browser's native behavior :

Although .trigger() simulates an event activation, complete with a synthesized event object, it does not perfectly replicate a naturally-occurring event.

If you add [0], you don't call jQuery's function but the standard DOM function, which perfectly works in this case.

like image 199
Denys Séguret Avatar answered May 27 '26 14:05

Denys Séguret


Because using $('#element')[0] gets the DOM element, therefore you can use javascript functions for that element

like image 38
Anton Avatar answered May 27 '26 16:05

Anton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!