What's the error here?[SOLVED] | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

What's the error here?[SOLVED]

I want to change color when we click on button. Why it isn't happening? https://sololearn.com/compiler-playground/Wmd39D8t74NG/?ref=app

20th Feb 2024, 1:18 PM
Harsh
Harsh - avatar
9 ответов
+ 2
You cannot assign as style to innerHTML. And textContent generally is preferred over inner HTML for changing text. https://sololearn.com/compiler-playground/WP75l6YyS494/?ref=app
20th Feb 2024, 2:11 PM
Lisa
Lisa - avatar
+ 2
Happens "click" is reserved in JS. You need to rename the function. (answer provided by Bing) Another issue is that you chained an assignment to the innerHTML of the element when you define x, which gives x the same value as that assigned to the innerHTML, that is "Heyo". You probably mean to assign the element to x and use the dot notation on x in the next two statements . Edit: The example provided by Lisa shows a work-around without changing the function name!🤯
20th Feb 2024, 2:32 PM
Lochard
Lochard - avatar
+ 1
Harsh https://www.w3schools.com/js/js_reserved.asp This page on w3school.com lists some reserved words. Maybe it is not complete since it doesn't mention "click", the one we are talking about.
21st Feb 2024, 12:45 PM
Lochard
Lochard - avatar
0
Lochard My head is spinning too much. Please simplify it.
21st Feb 2024, 12:20 PM
Harsh
Harsh - avatar
0
Lisa Then, how can we use onclick attribute of html? Why can't we go with it. Okay, I have brought change in question, let's remove the concept of changing the word. I am sticking to onclick attribute because, I saw it on w3school.com and it was quite easy as pie.
21st Feb 2024, 12:22 PM
Harsh
Harsh - avatar
0
You can use onclick (see updated example above). As Lochard pointed out, you should name your function differently.
21st Feb 2024, 12:30 PM
Lisa
Lisa - avatar
0
Lisa Now I understood perfectly. And also added some more functions. One more thing, can you tell me some reserved words which should not be used?
21st Feb 2024, 12:38 PM
Harsh
Harsh - avatar
0
Harsh You can simply change the function name to anything that is not a reserved word and it will work. You can also add the assignment to the innerHTML as a separate statement. It will also work. https://sololearn.com/compiler-playground/W5RBXm0JiWLF/?ref=app
21st Feb 2024, 12:38 PM
Lochard
Lochard - avatar
0
Lisa Excuse me Mentor, I have one more question regarding css and definitely not related to this code. It is from a sololearn project. So, should I ask it here or dm you?
21st Feb 2024, 1:04 PM
Harsh
Harsh - avatar