Is there a way around this?

Started by
1 comment, last by Craazer 20 years, 10 months ago
Hi heres a simple function wich should but two images to web page and display alert box whit greeting string when cursor is above the image(s). But it doesn''t work becose i comes undefined or if the i would be global it would always be 1 and so both alert boxes would show "Hi". So is there anyway to make this work in &#106avascript?
  
var msgs = new Array("Helloy","Hi!")

function Doit()
{
for(i=0; i < 2;i++)
{
document.write(''<p><img border="0" src="pic.bmp" width="32" height="32"'')
document.write(''OnMouseOver="alert(msgs[i])"></p>'') // doesn''t work becose i is undefined

/*
if(i == 0) document.write(''OnMouseOver="alert(msgs[0])"></p>'') 
if(i == 1) document.write(''OnMouseOver="alert(msgs[1])"></p>'') 
// this should work!? but it''s dirty coding...
*/
}
}
  
Advertisement
You have to do it this way:>


  var msgs = new Array("Helloy","Hi!")function Doit(){	for(i=0; i<2;i++)	{		document.write(''<p><img border="0" src="pic.bmp" width="32" height="32"'');		document.write(''OnMouseOver="alert(msgs[''+i+''])"></p>'');	}	}  
I am a signature virus. Please add me to your signature so that I may multiply.
quote:Original post by pag
You have to do it this way:>


    var msgs = new Array("Helloy","Hi!")function Doit(){	for(i=0; i<2;i++)	{		document.write(''<p><img border="0" src="pic.bmp" width="32" height="32"'');		document.write(''OnMouseOver="alert(msgs[''+i+''])"></p>'');	}	}    


Ooh nice trick, thanks pag!

This topic is closed to new replies.

Advertisement