Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3236132
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:35:12+00:00 2026-05-17T17:35:12+00:00

I’m doing a javascript assignment and have just learned that I can do it

  • 0

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.

This is the contents of my javascript function:

    rowsArray = $("#Table1 tr");

for (i=0;i<rowsArray.length;i++){
    numSeatsInRow = rowsArray[i].getElementsByTagName("img").length;                    // discovers the number of seats in row [i]
    for(j=0;j<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;
 }  
}

As you can see, I started converting to jQuery with the first line, but got stuck 🙂

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].

The function creates and moves through a 2D array changing the id and mouse events of a set of images.

Any thoughts would be much appreciated 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:35:12+00:00Added an answer on May 17, 2026 at 5:35 pm

    It can be improved further, but something like this would work:

    $("#Table1 tr").each(function(i) {
       $(this).find("img").each(function(j) {
         this.id = "row" + i + "Seat" + j;
         $(this).hover(function() { this.src = "images/taken.gif"; }, 
                       function() { this.src = "images/available.gif"; })
                .click(function() { 
                   var img = this;
                   this.src = "images/mine.gif";
                   $(this).unbind("mouseenter mouseleave"); 
                   $("#myTickets").html(function(i, h) { return h + img.id; });
                 });
       });
    });
    

    Using .each() and the index it passes as the first parameter to the callback shortens it a bit (you don’t need your own i and j, they’re already there) the rest is just jQuery conversion, e.g. .hover() for the mouse entering/leaving and .unbind() to remove those handlers it later.


    Here’s a bit more verbose but much more efficient version using .delegate():

    $("#Table1 tr").each(function(i) {
       $(this).find("img").each(function(j) {
         this.id = "row" + i + "Seat" + j;
       });
    });
    $("#Table1").delegate("tr img", "click", function() { 
      var img = $(this).addClass("mine").add("src", "images/mine.gif"); 
      $("#myTickets").html(function(i, h) { return h + img.attr("id"); });
    }).delegate("tr img:not(.mine)", "mouseenter", function() { 
      this.src = "images/taken.gif";
    }).delegate("tr img:not(.mine)", "mouseleave", function() { 
      this.src = "images/available.gif";
    });
    

    This attaches only 3 handlers to the <table> and listens for events to bubble up, rather than attaching 3 handlers per image, so it’s much cheaper on the page load side, and and infinitesimal difference on the runtime side.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.