Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using getElementbyId to set event handlers globally

 
John Walbaum
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone know how to get the value of currentImg in the following script assigned as the loop is made. Right now, it's assigning the value at the end of the loop. But I want to establish a permanent value for each "Img" in the array that can be passed to the function newWindow, so when the user clicks on a particular image, a new page with additional information opens up.


var currentImg = -1;
function changeImages(dir){
if(dir==-1) {
currentImg-=numDisplay*2;
if(currentImg<0 && dir)currentImg = theImg.length + currentImg;
}
for(i=0;i<numDisplay;i++){
currentImg++;
if(currentImg >= theImg.length)currentImg=0;
document.getElementById("Img" + i).src = theImg[currentImg][0];
document.getElementById("Img" + i).alt = theImg[currentImg][1];
document.getElementById("Img_text" + i).innerHTML = theImg[currentImg][1];
document.getElementById("Img" + i).onklick = function(){newWindow('currentImg')}
}
}
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not forget about you, I am swamped with work...

Why the single quotes around currentimage in the function declaration??

Eric
 
John Walbaum
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. I knew you were busy so I thought I would give the peanut gallery a try.

Re your question: The text you suggested did not work. I found a new syntax <a href="http://www.codingforums.com/archive/index.php?t-5958.html">here</a>, a forum where others were having the same problem. The brackets and single quotes were a workaround.

It seemed to clear up the problem I was having in that the onklick event handler now works. The trouble is that the onklick function value is wrong. It's kind of like a loop that runs to six and instead of assigning i(1) = 1, i(2) = 2, etc., it assigns i(1) = 6, i(2) = 6, etc. I can't figure out the problem, but guess it has to do with the variable type.
 
John Walbaum
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. I knew you were busy so I thought I would give the peanut gallery a try.

Re your question: The text you suggested did not work. I found a new syntax <a href="http://www.codingforums.com/archive/index.php?t-5958.html">here</a>, a forum where others were having the same problem. The brackets and single quotes were a workaround.

It seemed to clear up the problem I was having in that the onklick event handler now works. The trouble is that the onklick function value is wrong. It's kind of like a loop that runs to six and instead of assigning i(1) = 1, i(2) = 2, etc., it assigns i(1) = 6, i(2) = 6, etc. I can't figure out the problem, but guess it has to do with the variable type.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic