Design comparison
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:
- The code works until I click "Dismiss Message". Then it behaves like script doesn't exist. (Now)
- Dismiss button doesn't work at all with .addEventListener.
- 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
- @JaneMorozPosted over 1 year ago
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 whensuccess
shows up. To do so you need to givesuccess
elementposition: absolute
and switch betweendisplay:none
anddisplay: something else
instead of changingdocument.body.innerHTML
. - Another solution is to move
subscribeButton.addEventListener
logic intoonSubmit()
function for example, you will also need to addinputElement
andinvalidMessage
:
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@BBualdoPosted over 1 year ago@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@BBualdoPosted over 1 year ago@JaneMoroz I added
window.location.reload()
as I thought on the beginning but feel free to teach me the better way :)0@JaneMorozPosted over 1 year ago@BBualdo
I have't seen that
success
usesinputElement.value
. So you needinputElement.value
to be global variable.I think since you changing
document.body.innerHTML
, you can removeaddEventListener
step entirely and just go toindex.html
and addonclick="onSubmit()"
tobutton
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 positionedabsolute
is a good solution too. You would have to add it toindex.html
but initially it should be hidden. You can createvisible
class and then just attach it to the element after you submit and remove it afterdismiss
.Marked as helpful1@JaneMorozPosted over 1 year ago@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@BBualdoPosted over 1 year ago@JaneMoroz OK, now I understand :D thanks for your help and time :D
0 - So maybe one solution is to make sure that
Please log in to post a comment
Log in with GitHubJoin 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