O'Reilly Forums: Chap 6: Looping Through Onclick Seats In Mandango - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chap 6: Looping Through Onclick Seats In Mandango

#1 User is offline   seqfarmer 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 03-March 14

Posted 03 March 2014 - 04:49 AM

At the end of Chapter 6 we create the window.onload function. As written in the book we get something like this...


window.onload = function(){
document.getElementById("findseat").onclick = findSeat;

document.getElementById("seat0").onclick = function(evt){ showSeatStatus(0); };
document.getElementById("seat1").onclick = function(evt){ showSeatStatus(1); };
document.getElementById("seat2").onclick = function(evt){ showSeatStatus(2); };
.....
document.getElementById("seat34").onclick = function(evt){ showSeatStatus(34); };
document.getElementById("seat35").onclick = function(evt){ showSeatStatus(35); };
initSeats();
}

We basically have the same command written 36 times and everything works when I do this. I've tried doing this to see if I could simplify the code further...

for(var s=0;s<36;s++){
document.getElementById("seat" + s).onclick = function(evt){ showSeatStatus(s); };
}

This does not work. When I put alert statements in, it appears to go through all of the seats but then thinks that every seat is seat 36. What is going on here?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users