Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It can be improved further, but something like this would work:</p> <pre><code>$("#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; }); }); }); }); </code></pre> <p>Using <a href="http://api.jquery.com/each/" rel="nofollow"><code>.each()</code></a> and the index it passes as the first parameter to the callback shortens it a bit (you don't need your own <code>i</code> and <code>j</code>, they're already there) the rest is just jQuery conversion, e.g. <a href="http://api.jquery.com/hover/" rel="nofollow"><code>.hover()</code></a> for the mouse entering/leaving and <a href="http://api.jquery.com/unbind/" rel="nofollow"><code>.unbind()</code></a> to remove those handlers it later.</p> <hr> <p>Here's a bit more verbose but <em>much</em> more efficient version using <a href="http://api.jquery.com/delegate/" rel="nofollow"><code>.delegate()</code></a>:</p> <pre><code>$("#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"; }); </code></pre> <p>This attaches only 3 handlers to the <code>&lt;table&gt;</code> and listens for events to bubble up, rather than attaching 3 handlers <em>per image</em>, so it's much cheaper on the page load side, and and infinitesimal difference on the runtime side.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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