views:

374

answers:

4

Noob here - I'm working on a simple Javascript calculator, and I'm using lists (li) for buttons. I want the li background-color to change onClick, but then automatically change back half a second later. So basically I want the button click to flash the new color, not toggle it.

Is this possible?

Edit: This is what I have in my header now, but it's still not working:

$(li).click(function() {
    var $elem = $(this);
    var oldBG = $elem.css('backgroundColor'));
    $elem.css('backgroundColor', '#FFFFFF').delay(1000).css('backgroundColor', oldBG);
});

and my li tags:

<li class="key" onClick="calc('7')">7</li>
+2  A: 

Sure is possible!

Check out the documentation for setTimeout()

Here's a link to get you started.

http://www.w3schools.com/js/js_timing.asp

Keith Twombley
Thanks very much for the link, I'll read through it and try my luck. Somebody please upvote Keith on my behalf.
yatman
+5  A: 

Edit - seems delay is for animation functions, not .css or similar. Revised code:

$('.key').click(function() {
      var $elem = $(this);
       var oldBG = $elem.css('background-color');
       $elem.css('backgroundColor', '#FF0000');
        setTimeout(function() {
            $elem.css('background-color', oldBG);
            }, 1000);
     }); 

working sample

jqueryui lets you animate color now, might be worth a look..

Dan Heberden
@Dan Isn't it 'background-color' instead of 'backgrounColor' ?
mohang
css attribute is background-color - jquery can use both, but for an object it takes backgroundColor (cause you can't have hiphens in object names).. so either works.
Dan Heberden
That code makes a ton of sense, but it's not working! This is how I modified it:[edit: see code in my original post]I'm not sure why it doesn't work. My li class is "key", but I've also got a separate onClick function inside the li brackets. Could that be causing some interference?
yatman
My bad on the delay function - it was going from red to oldBG with no delay. Revised the code and updated the answer
Dan Heberden
Dan, that's awesome work. It's still not working for me, but since that Fiddle works, I'm sure the problem is with my code. I'll work it out, but thanks very much for the help!
yatman
Figured it out - thanks again!
yatman
A: 

If you are willing to change your markup so that each button is an <a> element (perhaps inside those li elements), you can use

a:active {

To change the way the buttons look when pressed. http://www.w3schools.com/CSS/css_pseudo_classes.asp

Victor Stanciu
A: 

You can add a background color style to the element when clicked, and use the setTimeout function to remove it later:

$(function(){
  $('li').click(function(){
    var e = $(this);
    e.css('background-color', 'red');
    window.setTimeout(function(){
      e.css('background-color', '');
    }, 500);
  });
});

(Note: There is a delay method in jQquery, but that is intended to be used with animation, not as a replacement for setTimeout.)

Working example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" lang="sv" xml:lang="sv">
<head>
<title>Test</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">

$(function(){
  $('li').click(function(){
    var e = $(this);
    e.css('background-color', 'red');
    window.setTimeout(function(){
      e.css('background-color', '');
    }, 500);
  });
});

function calc(digit) {
    var lcd = $('#lcd');
    lcd.text(lcd.text() + digit);
}

</script>

</head>
<body>

<div id="lcd"></div>

<ul>
  <li class="key" onClick="calc('1')">1</li>
  <li class="key" onClick="calc('2')">2</li>
  <li class="key" onClick="calc('3')">3</li>
  <li class="key" onClick="calc('4')">4</li>
  <li class="key" onClick="calc('5')">5</li>
  <li class="key" onClick="calc('6')">6</li>
  <li class="key" onClick="calc('7')">7</li>
  <li class="key" onClick="calc('8')">8</li>
  <li class="key" onClick="calc('9')">9</li>
  <li class="key" onClick="calc('0')">0</li>
</ul>

</body>
</html>
Guffa
Right, so I used your code but it's not working. What am I doing wrong, do you think? Your code makes sense. My li tag looks like this: <li class="key" onClick="calc('7')">7</li>, then I have your code in script tags in the header. onClick Conflict, maybe?
yatman
@yatman: I don't know why it fails in your code. I tried the code with your tag in Firefox 3.6.3 and Internet Explorer 8, and it works just fine. Both the calc function and the jQuery event handler is called. I added a working example above.
Guffa
Figured it out - thanks again!
yatman