views:

160

answers:

4

I have table which class is forum. My jquery code:

<script type="text/javascript">
    $(document).ready(function() {
        $('.forum').bind("mouseover", function(){
            var color  = $(this).css("background-color");

            $(this).css("background", "#380606");

            $(this).bind("mouseout", function(){
                $(this).css("background", color);
            })    
        })    
    })
</script>

It perfectly works, but is it possible to do it in more efficient way without var color = $(this).css("background-color");. Just after mouseout leave the previous background-color and remove #380606? Thank you.

+3  A: 

If you don't care about IE ≤6, you could use pure CSS ...

.forum:hover { background-color: #380606; }

With jQuery, usually it is better to create a specific class for this style:

.forum_hover { background-color: #380606; }

and then apply the class on mouseover, and remove it on mouseout. (Example)

$('.forum').hover(function(){$(this).toggleClass('forum_hover');});

If you must not modify the class, you could save the original background color in .data() (Example):

  $('.forum').data('bgcolor', '#380606').hover(function(){
    var $this = $(this);
    var newBgc = $this.data('bgcolor');
    $this.data('bgcolor', $this.css('background-color')).css('background-color', newBgc);
  });

or (Example)

  $('.forum').hover(
    function(){
      var $this = $(this);
      $this.data('bgcolor', $this.css('background-color')).css('background-color', '#380606');
    },
    function(){
      var $this = $(this);
      $this.css('background-color', $this.data('bgcolor'));
    }
  );   
KennyTM
No, I need this done in Jquery actually, it is just a one piece of code.
hey
A: 

This should be set directly in the CSS.

.forum {background-color: #123456}
.forum:hover {background-color: #380606}

If you are worried about the fact the IE6 will not accept hover over elements which are not links, you can use the hover event of jQuery for compatibility.

Andrea
A: 

Try this , its working and simple

HTML

​​​​​​​​​​​​​​​​​​​​​<html>
<head></head>
<body>
    <div class="forum">
        test
    </div>
</body>
</html>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Javascript

$(document).ready(function() {
    var colorOrig=$(".forum").css('background-color');
    $(".forum").hover(
    function() {
        //mouse over
        $(this).css('background', '#ff0')
    }, function() {
        //mouse out
        $(this).css('background', colorOrig)
    });
});​

css ​

.forum{
    background:#f0f;
}​

live demo

http://jsfiddle.net/caBzg/

JapanPro
your variable colorOrig will return null as the css property "background" selector is unreadable by jQuery... use "background-color" or "backgroundColor" instead
joberror
@joberror, thats right i have edited code, please revisit
JapanPro
+1  A: 

Set the original background-color in you CSS file:

.forum{
    background-color:#f0f;
}​

You don't have to capture the original color in jQuery. Remeber that jQuery will alter the style INLINE, so by setting the background-color to null you will get the same result.

$(function() {
    $(".forum").hover(
    function() {
        $(this).css('background-color', '#ff0')
    }, function() {
        $(this).css('background-color', '')
    });
});​
Marc Uberstein
That is a great answer! and it works perfectly... Tested in firebugs
joberror
Thanks, appreciated! If you want, you can include the jquery.color plugin. Then you have the ability to change color with animation. e.g. $(this).animate({backgroundColor:'#ff0'},400);
Marc Uberstein

related questions