-4

I am starting to learn jquery and I tried to change the background colour and alert it with callback and it doesn't work probably something in my syntax is missing...

$("#b").click(function(){
  $("body").css("background-color,yellow",function(){
     alert("background-color has changed successfully");
  });
});

Thanks in advance for the help! :)

Ashish Ratan
  • 2,838
  • 1
  • 24
  • 50
  • possible duplicate of [Jquery change background color](http://stackoverflow.com/questions/4283141/jquery-change-background-color) – Ashish Ratan Apr 12 '14 at 08:44
  • 2
    You forgot the `"` after _background-color_. This question should be closed. – jahroy Apr 12 '14 at 08:46
  • 2
    This question is based on a syntax error by a new programmer and is unlikely to be helpful to future visitors. – jahroy Apr 12 '14 at 08:47

3 Answers3

5
 $("#b").click(function(e){
  $("body").css("background-color","yellow");
  alert("background-color has changed successfully");
  e.preventDefault();
 });

You have missed " inside css's 'background-color' property.

Demo:

http://jsfiddle.net/L2PpG/

RGS
  • 5,131
  • 6
  • 37
  • 65
  • You should at least explain what you changed to help the OP understand his error. Otherwise this answer is pretty worthless. – jahroy Apr 12 '14 at 08:49
2

I dont exactly know whether the function you used second time would work or not but try using this

 $("body").css('background-color','yellow');
alert("COLOR CHANGED");
sss999
  • 528
  • 3
  • 14
  • thank you for the help but I already tried you'r asnwer, my question is: if there is a way to do it with callback like first i changes the background color and the alert on it. – Guy Solell Apr 12 '14 at 08:46
  • ah sorry. So what exactly do u want? First the background color to be changed and then the alert to appear? is that it? – sss999 Apr 12 '14 at 08:51
1

and if u want to alert the color of the element, in this case yellow then use this code rather than my previous one:-

 $("#b").click(function(){
         $("body").css('background-color','yellow');
    var color = $(this).css("background-color");

    alert("Color Changed to "+ color);
    });
sss999
  • 528
  • 3
  • 14