-4

When I use this simple function the result excuts for less then a second and reload the page I tried many different ways to avoid and execute it generally but it doesn't work.

submit.addEventListener('click', switchpage)

function switchpage() {
  if (mail.value != "") {
    deimsBox.style.display = "grid";
    section.style.display = "none";
  }

even this

function switchpage() {
  console.log('hello')
}

submit.addEventListener('click', switchpage)
function switchpage() {
  if (mail.value != "") {
    deimsBox.style.display = "grid";
    section.style.display = "none";
  }
function switchpage() {
  console.log('hello')
VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • 1
    Please include ALL the RELEVANT code (HTML, CSS, JavaScript) that your question relates to. It is unclear what you are even trying to describe here. What, exactly is the desired action and what is actually happening. It is unclear what you mean by "executes for less than a second". It may be that because you are submitting a form, the page reloads, but if your `form`'s `action` is set to the current page, then that's exactly what should happen. – Scott Marcus Aug 28 '23 at 13:22
  • Please add relevant JavaScript code/HTML markup/CSS to your question as a [mcve]. You can use the snippet tool `[<>]` in the question edit toolbar to help you. It looks like you're clicking a submit form on a form which would reload the page if you don't take measures to prevent that - but without the corresponding markup showing us that code in context we can't really help. – Andy Aug 28 '23 at 13:22
  • 1
    Is `submit` perhaps *submitting a form*? Are you just asking how to prevent a form submission in JavaScript? If the goal isn't to submit a form in the first place, why use a form with a submit button? – David Aug 28 '23 at 13:23
  • Please also read [ask], especially the section titled "Write a title that summarizes the problem". The current title summarizes nothing. – Heretic Monkey Aug 28 '23 at 13:24
  • 1
    1. if you have a form, use the submit event plus event.preventDefault() to execute stuff without submitting. 2. NEVER call ANYTHING "submit" in a form, id="submit" and name="submit" will obscure the submit event – mplungjan Aug 28 '23 at 14:01
  • 1
    For example: `document.getElementById('myForm').addEventListener('submit', (event) => { event.preventDefault(); const mail = e.target.mail.value.trim(); deimsBox.style.hidden = mail === ""; section.style.hidden = mail !== "" });` – mplungjan Aug 28 '23 at 14:04

0 Answers0