Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding a jQuery click event to each element within a foreach loop
    primarykey
    data
    text
    <p>I am building a plugin which matches an element, finds a link within it and makes the parent element go to that location upon a click.</p> <p>I have a loop in the main body:</p> <pre><code> return this.each(function(options) { $to_link = $(this); //matched object link_href = $('a', $to_link).attr('href'); //link location $($to_link,$parent) .click(function(){alert(link_href); window.location = link_href; return false;}) .attr('title','Jump to ' + link_href); }) </code></pre> <p>which I am running it against this HTML</p> <pre><code>&lt;div id="a"&gt;&lt;h2&gt;&lt;a href="/products/"&gt;Products&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;blah blah&lt;/p&gt;&lt;/div&gt; &lt;div id="b"&gt;&lt;h2&gt;&lt;a href="/thinking/"&gt;Thinking&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;liuhads&lt;/p&gt;&lt;/div&gt; </code></pre> <p>The problem I have is that the click function always results in jumping to the value of the last matched div's link although the title of the element has the correct value. </p> <p>To clarify, behavious should be:</p> <ul> <li><p>div#a has a title of "Jump to /products/" and when clicked on goes to /products/</p></li> <li><p>div#a has a title of "Jump to /thinking/" and when clicked on goes to /thinking/</p></li> </ul> <p>instead, what happens is:</p> <ul> <li><p>div#a has a title of "Jump to /products/" and when clicked on goes to <strong>/thinking/</strong> (the alert says /thinking/ too)</p></li> <li><p>div#a has a title of "Jump to /thinking/" and when clicked on goes to /thinking/</p></li> </ul> <p>ie div#a ends up with the wrong behaviour. Im guessing this is some kind of scope issue but for the life of me I cannot see it, help!</p>
    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.
 

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