0

EDIT: Just to clarify, I did remove the document.write and but it is still having the same effect.

This is the code I am using right now:

<script language='javascript' type='text/javascript'>
function Beginnings() {
document.getElementById("TalkWindow").innerHTML = document.write("<a href      '#' onclick='nicetomeetyou();'>Hi John, my name is Jill</a><br>")
}
function nicetomeetyou() {
document.getElementById("TalkWindow").innerHTML = document.write("<a href '#' onclick='Beginnings2();'>Same here</a><br>")
}
function Beginnings2() {
document.getElementById("TalkWindow").innerHTML = document.write("<a href '#' onclick='END();'>Lets do this again, shall we?</a><br>")
}

</script><HTML>
<BODY>  
<table>
<tr>
<td> <div id="TalkWindow"> <a href='#' onclick='Beginnings();'>Hello</a>   </div>
</td>
</tr>
</table>

But when I click the second option, "Hi John, my name is Jill", it adds "Same here" on a second line and then immediately reloads the page back to "Hello". Any thoughts on what's wrong with the code? Thanks!

user1924218
  • 99
  • 1
  • 10
  • 1
    You're trying to use both `.innerHTML` *and* `document.write()` at the same time. Pick one, not both. (Although.. [don't pick `document.write()`](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice).) – JJJ Feb 08 '15 at 21:49
  • Also related: [document.write clears page](https://stackoverflow.com/questions/10873942/document-write-clears-page) – t.niese Feb 08 '15 at 21:50
  • You may NEVER use document.write after page load. It will WIPE the page. Just remove the document.write and use the innerHTML only. Then you will need to add the event handlers. You should look into jQuery and delegation of event handlers – mplungjan Feb 08 '15 at 21:50
  • Got rid of the document.write. However, when I choose "Hi John, my name is Jill", it still clears it and says "Hello" again. – user1924218 Feb 08 '15 at 21:53
  • You have links that goes to # without returning false. You need to rethink the whole idea – mplungjan Feb 08 '15 at 21:54
  • 2
    in you functions you have `href '#'` this is not equal to `href='#'` but equal to `href='' '#'` as of that a click on those links will reload the page. You either need to write `href='#'` or prevent the default behavior for the click by return `false` (`onclick='nicetomeetyou();return false;'`) – t.niese Feb 08 '15 at 21:58
  • In what sense do I need to rethink the idea? I did also just add the return false, but it is still doing the same thing. – user1924218 Feb 08 '15 at 21:59
  • If adding `return false;` did not work, then you did something wrong. – t.niese Feb 08 '15 at 22:01
  • Holy Crud... That was it. It was the href='#'. I remember adding that in there and then for some reason I forgot it. Holy cow! LOL. Thanks for your help! – user1924218 Feb 08 '15 at 22:02
  • 1
    The pattern you've chosen isn't generally very good. If you're interested in improving the code you could post it to [codereview.se] for suggestions. – JJJ Feb 08 '15 at 22:06

0 Answers0