Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
197
rated 0 times [  198] [ 1]  / answers: 1 / hits: 16123  / 11 Years ago, thu, december 26, 2013, 12:00:00

Recently I found jQuery cannot trigger the native click event on an anchor tag when I'm clicking on other elements, the example below won't work:



html



<a class=js-a1 href=new.html target=_blank>this is a link</a>
<a class=js-a2 href=another.html target=_blank>this is another link</a>


javascript



$('.js-a1').click(function () {
$('.js-a2').click();
return false;
});


And here is the jsfiddle - 1. Click on the first link won't trigger native click on the second one.



After some searches, I found a solution and an explanation.



Solution



Use the native DOM element.



$('.js-a1').click(function () {
$('.js-a2').get(0).click();
return false;
});


And here is the jsfiddle - 2.



Explanation



I found a post on Learn jQuery: Triggering Event Handlers. It told me:




The .trigger() function cannot be used to mimic native browser events, such as clicking on a file input box or an anchor tag. This is because, there is no event handler attached using jQuery's event system that corresponds to these events.




Question



So here comes my question:



How to understand 'there is no event handler attached using jQuery's event system that corresponds to these events'?



Why is there not such corresponding event handler?



EDIT



I update my jsfiddles, it seems there's and error on the class name.


More From » jquery

 Answers
15

there is no event handler attached using jQuery's event system that corresponds to these events



This means, at this point of the learning material, no jQuery event handlers has been attached to these elements using .click(function() {} or .bind('click', function () {}), etc.


The no-argument .click() is used to trigger (.trigger('click')) a "click" event from jQuery's perspective, which will execute all "click" event handlers registered by jQuery using .click, .bind, .on, etc. This pseudo event won't be sent to the browser.



.trigger()


Execute all handlers and behaviors attached to the matched elements for the given event type.



Check the updated jsFiddle example, click on the two links to see the difference. Hope it helps.


[#73538] Tuesday, December 24, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pranavrorys

Total Points: 466
Total Questions: 87
Total Answers: 115

Location: Barbados
Member since Sun, Nov 27, 2022
2 Years ago
pranavrorys questions
Fri, May 27, 22, 00:00, 2 Years ago
Thu, Oct 28, 21, 00:00, 3 Years ago
Sat, May 30, 20, 00:00, 4 Years ago
Fri, Dec 20, 19, 00:00, 5 Years ago
;