Skip to main content
gamedev.net
🔒 Locked

[web] document.write()

Started by Daedalus AI 5 replies 1.1k views
Original Post
Daedalus AI
Daedalus AI
Can anyone tell me why this;

document.write("<img id='#[[1]]' onClick='changeImage('#[[1]]');' src='./pictures/back.GIF'>");



Prints the image to the screen, but doesn't call my &#106avascript function when I click on it, like it would without the document.write(). Thanks, Daed.
Verminox
Verminox
Check your quotes in your OnClick attribute.

onClick='changeImage('#[[1]]');'


The browser things the value of onClick ends when it encounters the first open-bracket because you have a second single quote there.

You can either:

a.) Use a combination of double and single quotes:

onClick="changeImage('#[[1]]');"


or

b.) Escape quotes with a slash

onClick='changeImage(\'#[[1]]\');'


Even with option (a), you would have to slash out the double quotes because you are originally using them for the document.write() itself.

--------------------------------------Amaze your friends! Astound your family! Kennify your text!
dave
dave
You are correct, however the 3 quote solution is also doable:

document.write("< mg id='#[[1]]' onClick='changeImage(`#[[1]]`);' src='./pictures/back.GIF'>");
Daedalus AI
Daedalus AI
I don't know why there are [] brackets around the number '1' on my post, but anyhow.
I also tried;

"... onClick='changeImage(" + "'#1'" + ");' ..."

But that doesn't work either, I'll try what you've just suggested, thanks.

[Edited by - Daedalus AI on October 6, 2006 2:36:27 PM]
Verminox
Verminox
Anyhow may I ask what:
#[[1]]

is all about? I don't think you can name IDs like that. Correct me if I'm wrong, but aren't ID's supposed to be just alpha-numeric, but beginning with a letter (or am I doing too much of C++)? Perhaps you are confusing yourself with the # used in CSS selectors?
--------------------------------------Amaze your friends! Astound your family! Kennify your text!
Mathachew
Mathachew
how about:

document.write('<img id="img1" onclick="changeImage(\'img1\');" src="./pictures/back.GIF">');


I have no idea what the brackets are all about (maybe to validate using id tags with a leading number??), but the above should work with no problem and shouldn't cause any confusion with the quotation marks. The syntax highlighting is not done correctly, but try this. It should leave no question about the code.
Daedalus AI
Daedalus AI
Thanks for the feedback guys, this is what worked;

document.write("<img id=\"#[[1]]\" onClick=\"changeImage('#[[1]]');\" src=\"./pictures/back.GIF\">");


Thanks again.

btw. Please ignore those [] brackets, they only appear when I post on here.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.