Dear Sirs,

I have been playing around with this so far, and couldn't get a good solution.
Well I have my simple code :

<html>
<body>


<a href="#" onClick="document.write('P');>P</a>

<div id="a">a</div>

</body>
</html>

Well, normally, this onClick event directs us to a new empty page showing a 'P'. I want this 'P' to be shown in the div tag below.

Could you please help me?

Thanks in advance,

Tibor

Recommended Answers

In the line <a href="#" onClick="document.write('P');>P</a> you are missing the closing " for the onclick event.

If you set the onclick listener to "document.getElementById('a').innerHTML += 'P'" then it should add a letter 'P' to the div each time you click on it.

<html>
<body> …
Jump to Post

All 4 Replies

You don't write a document in a paragraph. It's just something that you don't normaly do.
The "document.write" writes a new document over the previous. Not inside the existing one. Meaning you can't have another document in a document.

I believen you might want to read and try to use the "innerHTML" command instead.

In the line <a href="#" onClick="document.write('P');>P</a> you are missing the closing " for the onclick event.

If you set the onclick listener to "document.getElementById('a').innerHTML += 'P'" then it should add a letter 'P' to the div each time you click on it.

<html>
<body>
<a href="#" onClick="document.getElementById('a').innerHTML += 'P'">P</a>
<div id="a">a</div>
</body>
</html>

Thanks for your reply, I don't usually use Jscript, but innerHTML does nothing with my document, does it work alone, or do I have to put some other code after / before that?

@dcddruck Thank you it works now!

Can you elaborate on what you mean by "does nothing with my document"? In the examples that were provided to you, it should be evident that what is being done here is accessing the content between the starting and closing tag of the element with an id = "a". innerHTML is that property that you use to access the content.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.