nikhil nikhil - 2 months ago 4x
Javascript Question

Javascript function not making parmanent changes

i am calling a javascript function by onsubmit event. but when i click on submit button changes happen only for a second. here is code

<p id="demo"> hello </p>
<form id="form" onsubmit ="change()">
<input type="text">
<input type="submit">
function change(){document.getElementById("demo").style.display="none";

i want to make permanent changes after calling the function.


By default a form will cause a page refresh. So any changes you made to the page are lost when you reload it.

Instead of using a form with a submit button, just use a normal button. (Since you're not actually trying to submit a form.) Something like this:

<p id="demo"> hello </p>
<form id="form">
  <input type="text">
  <button type="button" onclick="change()" />

You can further modify it to remove the form entirely, which would of course require updating JavaScript and any CSS to account for the structural change.