rogerio Posted March 31, 2010 Report Share Posted March 31, 2010 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> Link to comment Share on other sites More sharing options...
jeffman Posted March 31, 2010 Report Share Posted March 31, 2010 (edited) 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 March 31, 2010 by Deirdre's Dad Link to comment Share on other sites More sharing options...
rogerio Posted March 31, 2010 Author Report Share Posted March 31, 2010 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>" Link to comment Share on other sites More sharing options...
jeffman Posted March 31, 2010 Report Share Posted March 31, 2010 Heavens no. I get so used to the shorthand we use around here that I don't stop to think it might be confusing. Link to comment Share on other sites More sharing options...
rogerio Posted March 31, 2010 Author Report Share Posted March 31, 2010 I guess I need to talk to Deirdre Link to comment Share on other sites More sharing options...
hellenk Posted May 24, 2011 Report Share Posted May 24, 2011 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!! Link to comment Share on other sites More sharing options...
hellenk Posted May 30, 2011 Report Share Posted May 30, 2011 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!! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now