Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

I don't understand JS behaviour, but FIXED

BBualdoβ€’ 540

@BBualdo

Desktop design screenshot for the Newsletter sign-up form with success message coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
2junior
View challenge

Design comparison


SolutionDesign

Solution retrospective


FIXED I just added window.location.reload() to Dismiss Message Button. But feel free to tell me how to do it better way :)

Hi, The design for mobile and desktop is finished (I know its messy but I made it quickly to submit the solution asap to get your help).

I've tried different methods and 3 of them has one of these issues:

  1. The code works until I click "Dismiss Message". Then it behaves like script doesn't exist. (Now)
  2. Dismiss button doesn't work at all with .addEventListener.
  3. Code doesn't work at all.

I'm not sure about .innerHTML replacement. It looks like the code doesn't find 'js-dismiss-button class for querySelector. I don't know. Please help.

Thank you :)

Community feedback

Eugeniaβ€’ 950

@JaneMoroz

Posted

Hello! Cool solution to the challenge πŸ‘

I've noticed that the submit button works the first time but not the second. So it seems like the problem is that after we click 'Dismiss message' and go back to the initial form, event listeners are not attached. I think this is because the form we're going back to is not the initial one but just newly created (with function pageSwapPrevious()).

  • So maybe one solution is to make sure that newsletter is not removed when success shows up. To do so you need to give success element position: absolute and switch between display:none and display: something else instead of changing document.body.innerHTML .
  • Another solution is to move subscribeButton.addEventListener logic into onSubmit() function for example, you will also need to add inputElement and invalidMessage:
function onSubmit() {
  const inputElement = document.querySelector('.js-input');
  const invalidMessage = document.querySelector('.invalid-email');

  const regx = /^([a-zA-Z0-9\._]+)@([a-zA-Z0-9])+.([a-z]+)(.[a-z]+)?$/
  
  if (inputElement.value.match(regx)) {
    pageSwapNext();
    return;
  } else {
    invalidMessage.innerHTML = 'Valid email required'
  } 
}

And then just add it to the button's onclick: <button onclick="onSubmit()" class="subscribe js-subscribe-button">Subscribe to monthly newsletter</button>

Keep it up! And good luck πŸ€

1

BBualdoβ€’ 540

@BBualdo

Posted

@JaneMoroz I don't think that positioning absolute is correct way to code it :/ Moving logic into onSubmit() function didn't solve the problem. There must be a way to return page content back to initial. First I thought about doing something to reload the page when I click Dismiss button but that is not the way too. It's weird. Any other ideas?

1
BBualdoβ€’ 540

@BBualdo

Posted

@JaneMoroz I added window.location.reload() as I thought on the beginning but feel free to teach me the better way :)

0
Eugeniaβ€’ 950

@JaneMoroz

Posted

@BBualdo

I have't seen that success uses inputElement.value. So you need inputElement.value to be global variable.

I think since you changing document.body.innerHTML, you can remove addEventListener step entirely and just go to index.html and add onclick="onSubmit()" to button there as well.

let inputElement = document.querySelector(".js-input");
let invalidMessage = document.querySelector(".invalid-email");

function onSubmit() {
  inputElement = document.querySelector(".js-input");
  invalidMessage = document.querySelector(".invalid-email");

  const regx = /^([a-zA-Z0-9\._]+)@([a-zA-Z0-9])+.([a-z]+)(.[a-z]+)?$/;

  if (inputElement.value.match(regx)) {
    pageSwapNext();
    return;
  } else {
    invalidMessage.innerHTML = "Valid email required";
  }
}
.
.
.
function pageSwapPrevious() {
.
.
.
<button onclick="onSubmit()" class="subscribe js-subscribe-button">Subscribe to monthly newsletter</button>
.
.
.
}

Everything works now!

P.S. Making success element positioned absolute is a good solution too. You would have to add it to index.html but initially it should be hidden. You can create visible class and then just attach it to the element after you submit and remove it after dismiss.

Marked as helpful

1
Eugeniaβ€’ 950

@JaneMoroz

Posted

@BBualdo

There're lots of ways to do it. window.location.reload() is not a bad solution, but it refreshes the whole page. If in future you have some long page with lots of stuff, reloading it can simply take too much time so in general people try to avoid using it.

1
BBualdoβ€’ 540

@BBualdo

Posted

@JaneMoroz Yeah, I also think it's temporary solution.

0
BBualdoβ€’ 540

@BBualdo

Posted

@JaneMoroz OK, now I understand :D thanks for your help and time :D

0

Please log in to post a comment

Log in with GitHub
Discord logo

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord