Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As Ionut G. Stan says, the issue is the closure over 'i'. RaYell is right about your wanting to declare the var (but not about that solving the problem).</p> <p>That loop also repeatedly re-executes the <code>$$</code> call, which isn't really ideal, and completely unnecessarily calls <code>$</code> to look up elements you've already looked up (via <code>$$</code>).</p> <p>FWIW:</p> <pre><code>Event.observe(window, 'load', function() { $$('ul#review_list li').each(function(elm, index) { ++index; // Now it's 1-based elm.observe('click', function(event) { alert(index); }); }); }); </code></pre> <p><code>$$</code> looks up the elements, <a href="http://prototypejs.org/api/enumerable/each" rel="nofollow noreferrer"><code>Enumerable#each</code></a> then iterates through the result calling the given function with the element reference and its zero-based index in the array. The event handler is then a closure over several things, including the <code>index</code> parameter passed into the <code>#each</code> iterator.</p> <p><em>Edit: I'm sorry, I just realized I made a massive assumption below: That the cover_x elements are, in fact, the list items under review_list. If they're not, disregard the below and my apologies! - T.J.</em></p> <p>So that works, but it's also unnecessarily complex. Event delegation could be your friend here: Look for clicks on the list, rather than list items:</p> <pre><code>Event.observe(window, 'load', function() { $('review_list').observe('click', function(event) { var li; li = event.findElement('li'); if (li) { // ...your code here to interact with the list item. // If you need the index, you can find it easily enough: // index = parseInt(li.id.substring(6)); // ...since your IDs on the LI items is "cover_x" with x // being the index. } }); }); </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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