document.getElementById("mytext").innerHTML = "Hello world";
<button onclick="showText()">Try it</button>
Because you didn't set the 'action' attribute on the <form> tag, it defaults to the page itself. So when you click on the button, the form is submitted and the page reloads itself. And since the page is so simple, it's practically instantaneous. The simplest solution is to cancel or negate the <button> action:
<button onclick="showText(); return false;">Try it</button>
Thanks, helps a lot.
I just read more about it, and seems like might be even better use:
instead of button as it works better on different browsers they say? Doing this anyway does not reload the page. But your message opened this to me.
<input type='button' value='Try it' onclick="showText()">
It's largely a distinction without a difference. You'll still need the 'return false;' to stop the form submission.
But isn't it that its not needed here as the type='button'. If the type is 'submit' then its needed? I guess in the original button it was submit as well as a default. When I am running this <input type='button' .. without return fals; it works on both browsers. But again, I learned a lot here.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread