setting innerHtml of div tag, changes aren't sticking

  • Thread starter Mad Scientist Jr
  • Start date
M

Mad Scientist Jr

I'm trying to set the inner html of a div tag (browser is IE6) - it
seems to work, but as soon as the function ends, the changes disappear.
How do you make the changes stick? Thanks

PS here is the code

<html>
<head>
<title>inner html test</title>
<script language="javascript">
<!-- begin script
function UpdateDiv ()
{
div1.innerHTML = '<h1>' + document.TestForm.txtInput.value + '</h1>';
div1.visibility = "show";
//document.getElementById('div1').innerHTML='<h1>' +
document.TestForm.txtInput.value + '</h1>';
alert("in IE6 after you click OK, the inner html disappears...");
}
// end script -->
</script>
</head>
<body>
<FORM METHOD="post" ACTION="" NAME="TestForm" ID="TestForm" >
type something: <input type="text" name="txtInput" id="txtInput"
value="hello" size="5"><br>
<input type="submit" value="click to display below in div tag"
onClick="UpdateDiv();"><br>
<div id="div1" name="div1"></div>
</FORM>
</body>
</html>
 
H

Hallvard B Furuseth

Mad said:
I'm trying to set the inner html of a div tag (browser is IE6) - it
seems to work, but as soon as the function ends, the changes
disappear. How do you make the changes stick?

After the <input ... onclick> code, the input button's default action
is performed - i.e. to post the form and thus reload the page.

Unless the onclick code returns false. So change the code to either
onclick="UpdateDiv(); return false;"
or to
onclick="return UpdateDiv()"
where you change the UpdateDiv() function to return false.
 
V

VK

Mad said:
I'm trying to set the inner html of a div tag (browser is IE6) - it
seems to work, but as soon as the function ends, the changes disappear.
How do you make the changes stick? Thanks

PS here is the code

<input type="submit" value="click to display below in div tag"
onClick="UpdateDiv();">

This is because you're using submit button and do not cancel submit
event. With action="" it leads to page reload - with all changes
disappearing.

Change <input type="submit" to <input type="button" and be happy (at
least for a while :)
 
M

Mad Scientist Jr

thanks for the advice!
This is because you're using submit button and do not cancel submit
event. With action="" it leads to page reload - with all changes
disappearing.

Change <input type="submit" to <input type="button" and be happy (at
least for a while :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top