0

I am extremely new to JavaScript and have no idea how to fix this. Right now if you click the submit button you'll get a the alert message but then when you close it all the entries are still there.

function contactform(){
 var fname = document.getElementById("fnameinput");
 var lname = document.getElementById("lnameinput");
 var email = document.getElementById("emailinput");
 var phone = document.getElementById("phoneinput");


fname.value && lname.value && (email.value || phone.value) ? alert 
("Thank you, " + fname.value + " " + lname.value + "\n" 
+ "We will get back to you shortly!") : alert ("Please fill in all fields.");
}


document.getElementById("submit").addEventListener("click", contactform, false,);

I want the entire page to reload after clicking the submit button.

  • Does this answer your question? [How to reload a page using JavaScript](https://stackoverflow.com/questions/3715047/how-to-reload-a-page-using-javascript) – MilesZew Nov 02 '19 at 19:59

1 Answers1

1

To reload a page, all you have to do is set window.location to ''

You can do it like this:

window.location = ''

or like this:

window.location.assign('')

You full function would look like this:

function contactform(){
 var fname = document.getElementById("fnameinput");
 var lname = document.getElementById("lnameinput");
 var email = document.getElementById("emailinput");
 var phone = document.getElementById("phoneinput");

    if(fname.value && lname.value && (email.value || phone.value)) {
        alert("Thank you, " + fname.value + " " + lname.value + "\n" + "We will get back to you shortly!");
        window.location.assign('');
    } else alert("Please fill in all fields.");
}

or, with a bit of refactoring:

function contactform(){
 const fname = document.getElementById("fnameinput");
 const lname = document.getElementById("lnameinput");

    if(fname.value && lname.value && (document.getElementById("emailinput").value || document.getElementById("phoneinput").value)) {
        alert(`Thank you, ${fname.value} ${lname.value}\nWe will get back to you shortly!`);
        window.location.assign('');
    } else alert("Please fill in all fields.");
}
MilesZew
  • 667
  • 1
  • 8
  • 21
  • @DeborahWaressen Your welcome :) After doing some research it looks like using `window.location.reload()` is a more standard way of doing it, but they both have the same result – MilesZew Nov 02 '19 at 22:03