Sign in to follow this  
Daedalus AI

[web] document.write()

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this