I have the following javascript function that updates a text within a div when a button is clicked (using an onclick() event) It works, but it immediately changes back to the old text.
function func()
{
var text = document.getElementById("text");
text.innerHTML = "Changed";
};
The HTML
<body>
<form>
<input type="submit" value="Add Text" onclick="func()"/>
</form>
<div id="text">
Text to Change
</div>
</body>
What am I missing? I also tried returning 'false' from the function but no luck.
You are actually submitting the form. Prevent that by adding return false to the onclick attribute:
<input type="submit" value="Add Text" onclick="func(); return false;"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With