my code is not working. I’m creating a variable then add event listener to it
btnColor=document.getElementById("first");
btnColor.addEventListener('click', changeBtnColor);
then I create a function that change the btn text color to red, if it’s bg color is transparent
let changeBtnColor = (btnColor) => {
if (btnColor.style.backgroundColor == "trasnsparent") btnColor.style.color = "red";
}
and html code
<button type="button" class="first_text" id="first" onclick="changeBtnColor()">text</button>
4
Answers
You are saying this function needs to have
btnColor
passed in.In the inline
onclick="changeBtnColor()"
you do not pass in the button reference.In the addEventListener
btnColor
is the event object.Next issue is
trasnsparent
is nottransparent
So with addEventListener you want to use currentTarget
There are a few issues in your code that are preventing it from working correctly. Let’s go through the problems and provide the necessary fixes:
The event listener is added to the button before the
changeBtnColor
function is defined. This will result in an error because the function doesn’t exist yet. To fix this, you can rearrange your code so that the function is defined before adding the event listener.In the HTML code, you are trying to call the
changeBtnColor
function directly from the onclick attribute. However, since the function expects an argument (btnColor
), you need to modify the way you call it. Instead of usingonclick="changeBtnColor()"
, you can remove the onclick attribute from the HTML and let the event listener handle the click event.HTML:
JS:
on
* handlers likeonclick
"transparent"
(browsers returned value might differ) – instead negate the color you’re expecting it to be ("red"
)Since using Element.style should be discouraged whenever possible, it’s a better idea to set the styles in CSS and use Element.classList in JS :
You are calling
changeBtnColor
in two different places and it is wrong for both of them.Your function expects a button element to be passed to it as an argument.
When you bind it as an event listener, the argument passed into it is a
MostEvent
object.You can extract the button from it using the
target
property or thecurrentTarget
property.When you are binding to an element directly,
currentTarget
is safer as it will get the element to which the event is bound rather than the one that is actually clicked on. In this particular case, since the button only contains text (i.e. no other elements) they will always be the same.If you were using event delegation then you would need to use
target
and combine it with something likeclosest
to see if the element clicked was either the one you wanted or a descendent of it.You call it again here, only this time you don’t pass any argument to it at all.
Don’t use
onclick
attributes. It’s the 21st century now.Then you have a second problem:
This will never be true because:
transparent
(andstyle
accesses the inline style, not the computed style).In general, when dealing with style in JS, it’s usually a good idea to modify the classes applied to the element and style them with CSS.