Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, it depends on how you want to do it. Basically, you have two options:</p> <ol> <li>Add a click listener for each tag matching your selector</li> <li>Add a click listener of the parent container, and figure out the element that was clicked inside the click handler function (event delegation)</li> </ol> <p>To add the same click-listener to multiple tags, do this:</p> <pre><code>$(selectorForElements).click(function (e) { // Click handling code here. // e is the event object // this inside the handler refers to the element that was clicked. }); </code></pre> <p>To use <a href="http://lab.distilldesign.com/event-delegation" rel="nofollow noreferrer">event delegation</a>, do something like this:</p> <pre><code>$(selectorForParentElement).click(function (e) { var target = $(e.target()); // Checked if we clicked the "correct" element, else traverse // up the DOM tree until we find the parent element we ARE interested in. if(!target.is(whatYouAreInterestedIn)) { target = target.parent(whatYouAreInterestedIn).eq(0); } // Your click handler for the target goes here }); </code></pre> <p>When possible it is better to use event delegation since it binds fewer listeners and thus consumes less memory and increases execution speed, especially on older browsers. You can read some about event delegation here:</p> <ul> <li><a href="http://lab.distilldesign.com/event-delegation" rel="nofollow noreferrer">http://lab.distilldesign.com/event-delegation</a></li> <li><a href="http://icant.co.uk/sandbox/eventdelegation/" rel="nofollow noreferrer">http://icant.co.uk/sandbox/eventdelegation/</a></li> <li><a href="http://www.sitepoint.com/blogs/2008/07/23/javascript-event-delegation-is-easier-than-you-think/" rel="nofollow noreferrer">http://www.sitepoint.com/blogs/2008/07/23/javascript-event-delegation-is-easier-than-you-think/</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload