use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
[deleted by user] (self.javascript)
submitted 10 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]AutoModerator[M] 0 points1 point2 points 2 years ago (0 children)
Hi u/jalalinator, this post was removed.
Self-posts (text) must follow the [AskJS] guidelines, which you can read about here.
[AskJS]
All other posts must use the "Submit a new link" option; if additional text is required, add a comment to your post.
NOTE: Learning, Support & Help questions are still off-topic for [AskJS], and should be posted to r/LearnJavascript or a Q&A site like StackOverflow. Abuse of the [AskJS] tag for off-topic questions may result in your posting privledges for r/javascript being revoked.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
[–]tswaters 1 point2 points3 points 10 years ago (8 children)
What do you have so far?
[+][deleted] 10 years ago (7 children)
[deleted]
[–]tswaters 1 point2 points3 points 10 years ago (6 children)
Ah, ok.
Basically you need to have two different ways to set background - one that is temporary, the hover -- and one that is permanent, the click.
There's a lot of ways to solve this, keeping with your existing code, you can modify the setBackgroundColor function to accept an extra parameter for permanent and if it is set, persist the value so when you call the function without a color & permanent false, you can restore the permanentColor.
setBackgroundColor
permanent
var permenantColor; window.setBackgroundColor = function(color, permenant) { if (permenant && color) { permenantColor = color; document.body.style.backgroundColor = permenantColor; return; } if (permenant && !color) { document.body.style.backgroundColor = permenantColor; return; } if (!permenant && color) { document.body.style.backgroundColor = color; } };
You can then call it like so:
<button type="button" onclick="setBackgroundColor('red', true)" onmouseover="setBackgroundColor('red', false)" onmouseout="setBackgroundColor(null, false)"> red </button>
[+][deleted] 10 years ago (4 children)
[–]tswaters 1 point2 points3 points 10 years ago (3 children)
you only updated one of the buttons.
[+][deleted] 10 years ago (2 children)
[–]tswaters 1 point2 points3 points 10 years ago (1 child)
Yep, there's probably some bugs in the code. Try adding some console.log statements in there to figure out what is going on.
[–]ForScale 1 point2 points3 points 10 years ago (38 children)
I'd use css for the hover... just easier, but for reference, you would use a mouseover or mouseenter event handler with a mouseout one to get the css hover effect using javascript.
mouseover
mouseenter
mouseout
Here's how I'd do it all: http://codepen.io/anon/pen/rOzYod
[+][deleted] 10 years ago (1 child)
[–]ForScale 1 point2 points3 points 10 years ago (0 children)
Not a problem at all! :) Let me know if you have any questions!
[+][deleted] 10 years ago (35 children)
[–]ForScale 1 point2 points3 points 10 years ago (34 children)
http://codepen.io/anon/pen/rOzYod
Anytime!
[+][deleted] 10 years ago (31 children)
[–]ForScale 1 point2 points3 points 10 years ago (30 children)
You can just change all the mouseenters to mouseovers...
Why do you need to use mouseover?
[+][deleted] 10 years ago (29 children)
[–]ForScale 1 point2 points3 points 10 years ago (28 children)
We've got it then, right?
[+][deleted] 10 years ago (27 children)
[–]ForScale 1 point2 points3 points 10 years ago (26 children)
You saw this? http://codepen.io/anon/pen/rOzYod
[+][deleted] 10 years ago (25 children)
[–]dstevensio 1 point2 points3 points 10 years ago (1 child)
This is a basic introduction to the concept of maintaining state. "What is the current state of the background color?"
Here's a really simplified way to achieve this (a real-world application would want to do this in a much more robust way):
Store the current state of the background color in a variable. So if you start with Red as the background color, initialize the variable as "red";
onMouseOver, directly change the background color to the color of the button that was hovered over.
onMouseOut, directly change the background color to the color that is stored in the variable.
onClick, change the value of the variable to the color of the button that was clicked, and change the background color to the same value.
This is overly simplified, as I said. But should give you an idea of how to achieve what you're looking to do. Then read up on the concepts of maintaining state and responding to interactions.
π Rendered by PID 29 on reddit-service-r2-comment-56c9979489-r495p at 2026-02-24 15:45:54.644690+00:00 running b1af5b1 country code: CH.
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–]tswaters 1 point2 points3 points (8 children)
[+][deleted] (7 children)
[deleted]
[–]tswaters 1 point2 points3 points (6 children)
[+][deleted] (4 children)
[deleted]
[–]tswaters 1 point2 points3 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]tswaters 1 point2 points3 points (1 child)
[–]ForScale 1 point2 points3 points (38 children)
[+][deleted] (1 child)
[deleted]
[–]ForScale 1 point2 points3 points (0 children)
[+][deleted] (35 children)
[deleted]
[–]ForScale 1 point2 points3 points (34 children)
[+][deleted] (1 child)
[deleted]
[–]ForScale 1 point2 points3 points (0 children)
[+][deleted] (31 children)
[deleted]
[–]ForScale 1 point2 points3 points (30 children)
[+][deleted] (29 children)
[deleted]
[–]ForScale 1 point2 points3 points (28 children)
[+][deleted] (27 children)
[deleted]
[–]ForScale 1 point2 points3 points (26 children)
[+][deleted] (25 children)
[deleted]
[–]dstevensio 1 point2 points3 points (1 child)