Jump to content


Photo

Chap 6: Looping Through Onclick Seats In Mandango


  • Please log in to reply
3 replies to this topic

#1 seqfarmer

seqfarmer

    New Member

  • Members
  • Pip
  • 1 posts

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?

#2 honx

honx

    New Member

  • Members
  • Pip
  • 3 posts

Posted 24 September 2014 - 05:18 AM

same problem here. i also had the idea to optimize seat image event wiring to kill those 36 entries.
i also tried a simple for loop like this:

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

this doesn't work for me also. with this for loop every single seat has seatNum 4.
where's the problem in this code? i don't get it!!!

Edited by honx, 24 September 2014 - 05:20 AM.


#3 honx

honx

    New Member

  • Members
  • Pip
  • 3 posts

Posted 29 September 2014 - 04:21 AM

bump?

#4 honx

honx

    New Member

  • Members
  • Pip
  • 3 posts

Posted 23 October 2014 - 01:41 AM

bump?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users