Hi guys, just wanted an assessment for the test your skill page [link](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Test_your_skills:_Events)
and here is a link to my solution
Hi @Olixpin and welcome back
Great work on this task.
You don’t need the if
statement. It’s always true
. What you are basically doing is:
- Assigning the attribute to the
color
variable - Checking if the attribute was assigned to the
color
variable
You could just always call the line within.
Have a nice day,
Michael
PS: Your email address is visible in your post. I think it somehow ended up in the optional “name” field of your profile. I recommend removing it.
Since DOM understand color in text, which means, it will work when you do this in css file.
backgroundColor: ‘red’
my solution is
const changeBackgroundColor = (event) => {
const color = event.target.textContent;
document.body.style.backgroundColor = color;
}
and apply this to the button click event.
Hi @yumikohey and welcome to the community
You’re absolutely right. In this specific situation using textContent
would also work.
In general, using a data attribute feels a bit more independent to me. Think about using this in a bigger project and some times later you decide to change the text without remembering that it’s also used for JS. Having the info separately in a data attribute helps reminding you of this fact.
Have a nice day,
Michael