Design comparison
Solution retrospective
Script tag is in HTML, have no idea if it is the best practice, but it's so small, I figured that it don't matter. But I con not make the button another color on mobile. It pissed me off :D This project was a little challenge for me.
Fell free to give some feedback how I could do it better.
Community feedback
- @MishaHernandezPosted over 3 years ago
Hello TrakaMeitene π
-
Adding javascript in the html is not a good practice, but if the script is very small then there is no problem and rather it turns out to be more efficient.
-
To change the button you just need to add to the script a constant that gets the button's
id
and then inside the condition where the button must change color add the styles. For example:
var x = document.getElementById ("pop"); const btnShare = document.getElementById ("btnShare") if (x.style.display === "none") { x.style.display = "block"; btnShare.style.backgroundColor = "hsl(217, 19%, 35%)"; btnShare.color = "white"; } else { x.style.display = "none"; btnShare.style.backgroundColor = "hsl(210, 46%, 95%)"; btnShare.color = ""; }
As additional observations:
- To initialize a variable use
const
orlet
instead ofvar
. - To add styles from javascript I recommend using
classList
which gives you more options. [HTML DOM classList Property] (https://www.w3schools.com/jsref/prop_element_classlist.asp)
I hope I can help you with these recommendations. Happy Coding β
1@TrakaMeitenePosted over 3 years ago@MishaHernandez <code> function myFunction() { const x = document.getElementById("pop"); if (x.style.display ="block") { x.classList.add("pop-content"); } }</code>
I took off the <code> pop-content </code> class and made this script to test it's function. Made it a lot easier to understand.
Thank You!
1 -
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