Topic: onClick inside document.write() (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=21333" title="Pages that link to Topic: onClick inside document.write() (Page 1 of 1)" rel="nofollow" >Topic: onClick inside document.write() <span class="small">(Page 1 of 1)</span>\

 
asptamer
Paranoid (IV) Inmate

From: The Lair
Insane since: Apr 2003

posted posted 04-14-2004 04:24

I am trying to figure out why

code:
document.write('<img src="empty.gif"  onClick="src="black.gif"">');



doesnt work, and particularly

code:
document.write("<TD> <img src="+goBoard[i][j].src+" onClick='src="+black.src+"></TD>");



where goBoard[x][y] holds an image and black is a variable that that holds a different image.
Both strings work perfectly without the onClick function, but both break down when onClick is inserted. I am trying to change the source of the image when it is clicked. I need to use document.write since Im creating HTML table filled with images out of JavaScript.

So my main question is why does onClick give me trouble when using it inside document.write in this case? Please help

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 04-14-2004 05:24

asptamer: Hi, it seems to be a simple problem of quotes escaping.
The first document.write should be

code:
document.write( '<img src="empty.gif"  onClick="src=\\'black.gif\\'">' );

and the second

code:
document.write("<TD> <img src='"+ goBoard[i][j].src +"' onClick='src=\\""+ black.src +"\\"' ></TD>");

I've tested the 2 with success with IE6.

[edit] HINT: To avoid that kind of problem in the future, always double check the HTML you generate by throwing it in an alert( ) or a textarea. [/edit]

Cheers,


(Edited by poi on 04-13-2004 20:27)

asptamer
Paranoid (IV) Inmate

From: The Lair
Insane since: Apr 2003

posted posted 04-14-2004 10:25

thanks : )



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:

« BackwardsOnwards »

Show Forum Drop Down Menu