Design comparison
Solution retrospective
Good evening, how do I do so that when I'm not selected an alert skips and when I'm selected the number skips, I did it both ways but when I do it in one it works for me and if I put another the other works for me! how would it be?
Community feedback
- @nicodes-devPosted over 1 year ago
Hi Lucca! In your code, you do not have an element with an id 'no select'.
const create = document.getElementById('no select')
So this code assigns a null value in your variable create. Thus in your if else statement, it will always be true.
// boton.addEventListener('click', () => { // const create = document.getElementById('no select') //returns null // if (create === null) { //null === null // alert('selecciona un valor') // } else { // centro2.classList.remove('.segundo') // centro1.style.display = 'none' // centro2.style.display = 'flex' // } // })
On your other if else statement, you are using a logical AND (&& ) operator to test two conditions.
create === null && create === botones
This will always return false, so only the else statement will be applied.
boton.addEventListener('click', () => { const create = document.getElementById('no select') if (create === null && create === botones) { //always false alert('selecciona un valor') } else { centro2.classList.remove('.segundo') centro1.style.display = 'none' centro2.style.display = 'flex' } })
A simple fix is to use the element which you display when a button is clicked. You can check if its innerHTML is not an empty string.
Numero.innerHTML.length === 0
You can't check if it is null because the type of Numero.innerHTML is a string
console.log(typeof Numero.innerHTML) //returns string
Now your code should be working fine if you use this in your if else statement.
boton.addEventListener('click', () => { if (Numero.innerHTML.length === 0) { alert('selecciona un valor') } else { centro2.classList.remove('.segundo') centro1.style.display = 'none' centro2.style.display = 'flex' } })
I hope this helps you with your solution.
Marked as helpful0@LuccaMauroMolinaPosted over 1 year ago@nicodes-dev good morning, I tried it there and it works, thank you very much I appreciate it and I will take into account what you told me
0
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