14

I want to Change the background colour on click . This is my code work that i tried.pls help me out :)

   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">

$(document).ready(function(){

$(#co).click(change()
{
$(body).css("background-color":"blue");
});
}); 

Css code

body
{
background-color:red;
}

Body code

      <body>

    <div id="co" click="change()">

hello

    </div>

8 Answers 8

27

You're using a colon instead of a comma. Try:

$(body).css("background-color","blue");

You also need to wrap the id in quotes or it will look for a variable called #co

$("#co").click(change()

There are many more issues here. click isn't an HTML attribute. You want onclick (which is redundant). Try this:

<div id="co"> <!-- no onclick method needed -->
<script>
$(document).ready(function() {
    $("#co").click(function() {
        $("body").css("background-color","blue"); //edit, body must be in quotes!
    });
});
</script>

You were trying to call an undefined method. It looks like you were trying to declare it inside the callback statement? I'm not sure. But please compare this to your code and see the differences.

http://jsfiddle.net/CLwE5/ demo fiddle

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

5 Comments

Taking a second look.. there are issues everywhere in your code. I will update my answer
Cannot edit the valid solution gived by RUJordan, it only need to close the script tag...
@SunnyCoder be more specific. What does the error console say? Not working doens't tell me much
Found it. body needs to be in quotes. See my edit, I included a working demo
@SunnyCoder because jQuery is handling the click event. So if you added the onclick back, it would be redundant.
3

Try this

$("body").css({"background-color":"blue"}); 

Comments

3
$("#co").click(function(){
   $(this).css({"backgroundColor" : "blue"});
});

4 Comments

please describe your answer so it'll make sense for all user.
what do you mean? when user clicks on the id named "co" the background color of that changes to blue!
Totally agrees with you but for new user or fresher who don't know anything about css or jquery for them need description. Hope this make sense.
mmm... you're right but this user used jquery in the code. means he/she knows something about that :)
1

The code below will change the div to blue.

<script>
 $(document).ready(function(){ 
   $("#co").click({
            $("body").css("background-color","blue");
      });
    }); 
</script>
<body>
      <div id="co">hello</div>
</body>

1 Comment

You are missing quotes around #co
0

1.Remove onclick method from div element

2.Remove function change() from jQuery code and in place of that create an anonymous function like:

$(document).ready(function()
{

  $('#co').click(function()
   {

  $('body').css('background-color','blue');
  });
});

Comments

0
$("#bchange").click(function() {
    $("body, this").css("background-color","yellow");
});

1 Comment

Your answer is under review for being a low quality answer. Please provide an explanation to your code.
0

Change Background Color using on click button jquery

Below is the code of jquery, which you can put in your head tag.

jQuery Code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
      $("button").click(function(){
        $("div").addClass("myclass");
      });
    });
</script>

CSS Code:

<style>
    .myclass {
        background-color: red;
        padding: 100px;
        color: #fff;
    }
</style>

HTML Code:

<body>
    <div>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div>
    <button>Click Here</button>
</body>

1 Comment

Your profile indicates you're associated with the company that owns the site to which you have linked. Linking to something you're affiliated with (e.g. a product or website) without disclosing it's yours in your post is considered spam on Stack Exchange/Stack Overflow. See: What signifies "Good" self promotion?, some tips and advice about self-promotion, What is the exact definition of "spam" for Stack Overflow?, and What makes something spam.
0

Try below jQuery snippet, you can change color :

<script type="text/javascript">
    $(document).ready(function(){
        $("#co").click(function() {
            $("body").css("background-color", "yellow");
        });
    });
</script>

$(document).ready(function(){
    $("#co").click(function() {
        $("body").css("background-color", "yellow");
    });
});
body {
    background-color:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="co" click="change()">hello</div>

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.