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

 
Author Thread
asptamer
Paranoid (IV) Inmate

From: The Lair
Insane since: Apr 2003

IP logged posted posted 04-14-2004 04:24 Edit Quote

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

IP logged posted posted 04-14-2004 05:24 Edit Quote

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

IP logged posted posted 04-14-2004 10:25 Edit Quote

thanks : )



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

« BackwardsOnwards »

Show Forum Drop Down Menu