0

I want to change the css attribute of a div on hover like

$(function(){
$("div#box").hover({
$("div#box").css({z-index:'12'});
});
});

Dreamweaver is giving me a syntax error. Can anyone please tell what is the problem?

4 Answers 4

4

Try

$(function(){
    $("div#box").hover(function(){
        //hover on
        $("div#box").css({zIndex:'12'});
    },
    function(){
        //hover off
        $("div#box").css({zIndex:''});
    });
});

http://jsfiddle.net/ZtLLg/

Sign up to request clarification or add additional context in comments.

1 Comment

thanks that works perfectly.I hope it is same for attributes such as borderBottom for border-bottom ?
3
  1. At the beginning, I think you want $(, not $.(.
  2. At the end of the second line, I think you want another nested function: .hover(function(){ instead of .hover({. As it is, you're creating an object literal, which I don't think is what you want.

1 Comment

$(function(){ $("div#box").hover(function(){ $("div#box").css({'z-index','12'}); }); }); It still giving syntax error on the .css line
2

Try this

$(function(){
$("div#box").hover(function(){
$("div#box").css('z-index', '12');
});
});

http://api.jquery.com/ready/

http://api.jquery.com/css/

3 Comments

The css call shouldn't need to be changed -- it can take a map (object literal).
@JoeWhite - I think it needs to be a string. jsfiddle.net/jvkjF/1 ({zIndex:12} works though, jsfiddle.net/jvkjF/2)
Ah, yes, you're right about it needing a string (because of the hyphen in z-index). I only noticed that you had changed it from the map overload to the two-strings overload -- I missed the bit where you had also put quotes around the z-index.
0

Altogether, I have just commented how to use animation and classes.

$(document).ready(function() {
   $('div#box').hover(
      function(){
        $("div#box").css({zIndex:'12'});
        //$(this).find('img').animate({opacity: ".6"}, 300);        
        //$(this).find('.caption').animate({top:"-85px"}, 300);
        //$(this).removeClass().addClass("someClassName");          
      }, 
      function(){
        $("div#box").css({zIndex:''});
        //$(this).find('img').animate({opacity: "1.0"}, 300);                   
        //$(this).find('.caption').animate({top:"85px"}, 100);
        //$(this).removeClass().addClass("someClassName");          
      }     
      );
});

Comments

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.