[web] document.write()

This topic is 4371 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 javascript function when I click on it, like it would without the document.write(). Thanks, Daed.

Share on other sites
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.

Share on other sites
You are correct, however the 3 quote solution is also doable:

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

Share on other sites
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]

Share on other sites
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?

Share on other sites

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.

Share on other sites
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.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 33
• Forum Statistics

• Total Topics
632634
• Total Posts
3007546
• Who's Online (See full list)

There are no registered users currently online

×