1

Basically a toggle. I have this button:

<button id='btn'>Hover, don't click</button>

And this js:

function changeColor () {
    document.getElementById('btn').style.color = 'red';
}

Now:

document.getElementById('btn').addEventListener('mouseover', changeColor);

Worked as commanded, but:

document.getElementById('btn').removeEventListener('mouseout', changeColor);

Disobedient. JS Fiddle thought all are behaving correctly: http://jsfiddle.net/jimges/roq9x1os/

I disagree. Thanks!

3
  • you are adding mouseover, and removing mouseout Commented Nov 10, 2014 at 2:44
  • @Papa, I know how to do it in CSS. I just like to learn how js controls the DOM. Thanks for the suggestion Commented Nov 10, 2014 at 5:08
  • @Piwakawaka, so the two then had to be identical, except that one has the removeEventListener method. I tried that but nothing happens or, should I say, only the removeEventListener is executed as I don't see any changes taking place. I guess it requires another element (another button likely) as a control "switch". Thanks! Commented Nov 10, 2014 at 5:11

2 Answers 2

1

Try this:

Fiddle

var btn = document.getElementById('btn')
btn.onmouseover = function changeColor() {
    document.getElementById('btn').style.color = "#ff1033";
}
btn.onmouseleave = function changeColor() {
    document.getElementById('btn').style.color = "#fff";
}

Or using CSS it's much easier:

Fiddle

#btn {
    color: #ff1033;
}
#btn:hover {
    color: #fff;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Huh, I didn't know you can do events directly like that. Thanks a lot! yeah, CSS is the way! :)
Yes you can and You're Welcome! :)
0

Using onmouseover or css is much easier, but if you still want to do it with add Event listener, you must think that add will attach the function and remove will remove it from the element.

In any case are two separated events that you must add.

Example:

var element=document.getElementById('btn');
var changeColorA = function () {
    document.getElementById('btn').style.color = "#ff1033";

}
var changeColorB = function () {
            document.getElementById('btn').style.color = "#000";
}
element.addEventListener('mouseover', changeColorA);
element.addEventListener('mouseout', changeColorB);

Again, I DO NOT RECOMMEND YOU USE THIS CODE, IS FOR EDUCATIONAL POURPOSES ONLY.

Do it with CSS

3 Comments

Thanks @Leandro for taking the time to set examples. I do know how to do it in CSS. I only wanted to learn how js controls the DOM.
You're welcome James. Please take a second to upvote the answers useful to you and mark as response to the answer that really help you. This will increase your points and will increase your probability that other guys answer you the next time.
the site is not letting me make votes at this time. I need at least 15 reputations; currently, I only have 9, and that is up from the 4 I had last time ... though I don't know why. Know that I want to upvote each time an answer to my question is provided. Once I will reach the minimum, then yes, I will be upvoting. Thanks once more!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.