Jump to content
Sign in to follow this  
rogerio

innerHTML creating new window

Recommended Posts

When this code is run and the button clicked, it wipes out the existing code; how can this be prevented?

<html>	<head>		<title>Use script to create html tags</title>		<script type="text/javascript">			function Italic()			{   document.getElementById("test").innerHTML="<p><i> Some italic text.</i>";			}		</script>	</head>	<body bgcolor="white" id="test">		<b>Some bold text.</b>		<input type="button" value="Push Me" onclick="Italic()" />	</body></html>

Share this post


Link to post
Share on other sites

Use a concatenation operator instead of a simple assignment operator. I mean += instead of plain =, as inelement.innerHTML += "Hello";which is shorthand for element.innerHTML = element.innerHTML + "Hello";The innerHTML now contains everything it contained before, and "Hello" is appended to it.A more complicated answer is to target a container element more specifically. But I see this is a test run, and you may already be thinking in that direction.

Edited by Deirdre's Dad

Share this post


Link to post
Share on other sites

Is: element.innerHTML a legal statement? I had to spell it out before it would work: document.getElementById("test").innerHTML+="<p><i> Some italic text.</i>"

Share this post


Link to post
Share on other sites

Heavens no. I get so used to the shorthand we use around here that I don't stop to think it might be confusing.

Share this post


Link to post
Share on other sites

It is really such a help that we can come here with our doubts and get helpful suggestions and advice for our confusions which will help us to save a lot of time and effort!! It was really interesting to read the solution to innerHTML creating a new window and how to add text to the already existing one. The answer seemed to have bee rather simple and I am sure that we’ll have more of such similar doubts here!!

Share this post


Link to post
Share on other sites

It is really such a help that we can come here with our doubts and get helpful suggestions and advice for our confusions which will help us to save a lot of time and effort!! It was really interesting to read the solution to innerHTML creating a new window and how to add text to the already existing one. The answer seemed to have bee rather simple and I am sure that we’ll have more of such similar doubts here!!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...