Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert javascript array handling to jQuery
    primarykey
    data
    text
    <p>I'm doing a javascript assignment and have just learned that I can do it in jQuery if I wish, rather than vanilla javascript. I thought I'd give it a go to see what it's like.</p> <p>This is the contents of my javascript function:</p> <pre><code> rowsArray = $("#Table1 tr"); for (i=0;i&lt;rowsArray.length;i++){ numSeatsInRow = rowsArray[i].getElementsByTagName("img").length; // discovers the number of seats in row [i] for(j=0;j&lt;numSeatsInRow;j++) { // loops round once for each set in row [i] var currentSeat = rowsArray[i].getElementsByTagName("img")[j]; currentSeat.setAttribute("id","row" + i + "Seat" + j); currentSeat.onmouseover = function(){this.src = "images/taken.gif"}; currentSeat.onmouseout = function(){this.src = "images/available.gif"}; currentSeat.onclick = function() { this.src = "images/mine.gif"; this.onmouseover = null; this.onmouseout = null; document.getElementById("myTickets").innerHTML += this.id; } } </code></pre> <p>As you can see, I started converting to jQuery with the first line, but got stuck :)</p> <p>The code works as is, but I figure there must be a more elegant way of doing it in jQuery. I tried using $.each but my selectors or syntax was wrong and it didn't work. I have a vague idea how it works for a 1D array, but I'm not clear on how to iterate through the items in nested arrays i.e. array[x][y].</p> <p>The function creates and moves through a 2D array changing the id and mouse events of a set of images.</p> <p>Any thoughts would be much appreciated :)</p>
    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. 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