2

For some reason when I use jQuery .css() it won’t change my CSS. I've checked it through several times, and the syntax seems to be right.

I can see that it is running the operation, so the only thing that does not work is the CSS line:

jQuery:

var document = this;

jQuery(document).ready(function($){
    console.log("document ready");


jQuery("#buttonrow .button").click(function(){
    var $this = $(this);

    console.log("button clicked");
    if($this.attr("id") == 'redbutton'){
        console.log("red");
        $('#redCheckDiv').css('display', 'block');
    } else {
        console.log("blue");
        $('#blueCheckDiv').css('display', 'block');
    }
});

});

HTML

<div class="row" id="buttonrow">
    <div class="col-md-5 col-md-offset-1 button" id="bluebutton">
    <div class="blueCheckDiv"></div>
        <div class="col-md-10 col-md-offset-1 text-center" id="text-container">
            <p>This is the blue box</p>
        </div>
    </div>
    <div class="col-md-5 button" id="redbutton">
    <div class="redCheckDiv"></div>
        <div class="col-md-10 col-md-offset-1 text-center" id="text-container">
            <p>this is the red box</p>
        </div>
    </div>
</div>
5
  • what functionality you want to achieve? Commented Nov 19, 2016 at 11:16
  • 3
    You are using # id selector, but in your html code attribute is class, so, use class selector., e.g. : $('.redCheckDiv')... Commented Nov 19, 2016 at 11:16
  • seems like its the element selector pbm.... Commented Nov 19, 2016 at 11:18
  • Lol yea, i'm using id selector... Haha, sorry for not realising that before posting. Commented Nov 19, 2016 at 11:18
  • It works now, after having fixed the selectors. Commented Nov 19, 2016 at 11:19

4 Answers 4

6

So here in your html, You have created redcheckdiv and bluecheckdiv as class not as id. So you haveto use dot instead of # . Below is the answer.

var document = this;

jQuery(document).ready(function($){
    console.log("document ready");


  jQuery("#buttonrow .button").click(function(){
    var $this = $(this);

    console.log("button clicked");
    if($this.attr("id") == 'redbutton'){
        console.log("red");
        $('.redCheckDiv').css('display', 'block');
    } else {
        console.log("blue");
        $('.blueCheckDiv').css('display', 'block');
    }
  });

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

Comments

0

As you already know, your selector was invalid, because you selected on an ID, but a class was defined in your markup. This can be easy to do, and hard to spot, even for experienced JavaScript developers.

Avoiding pain associated with bad selectors

To debug this sort of issue, use the debugger console. Enter the selector(s) for the problematic area of your code, and hit enter:

$('#blueCheckDiv')

If you get this result, the selector does not exist on the page.

j…y.fn.init {context: document, selector: "#blueCheckDiv"}

If your markup exists before your JavaScript does, you may find it’s easiest to build your selectors this way. Enter them into he console to test them, before even adding them to your JavaScript code.

Comments

0

Use the below code it's working perfectly ( Change Id to class )

<script type="text/javascript">
$(document).ready(function() {
    $("#buttonrow .button").click(function() {

        var click_id = $(this).attr("id");

        if (click_id == 'redbutton') {
            console.log("red");
            $('.redCheckDiv').css('display', 'block');
        } else {
            console.log("blue");
            $('.blueCheckDiv').css('display', 'block');
        }
    });

});
</script>

Comments

0

You entered 2 div's with classes in the HTML, but you marked them as id's in the jQuery, here is the correct code:

$(document).ready(function() {
    $("#buttonrow .button").click(function() {

        var click_id = $(this).attr("id");

        if (click_id == 'redbutton') {
            console.log("red");
            $('.redCheckDiv').css('display', 'block');
        } else {
            console.log("blue");
            $('.blueCheckDiv').css('display', 'block');
        }
    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row" id="buttonrow">
    <div class="col-md-5 col-md-offset-1 button" id="bluebutton">
    <div class="blueCheckDiv"></div>
        <div class="col-md-10 col-md-offset-1 text-center" id="text-container">
            <p>This is the blue box</p>
        </div>
    </div>
    <div class="col-md-5 button" id="redbutton">
    <div class="redCheckDiv"></div>
        <div class="col-md-10 col-md-offset-1 text-center" id="text-container">
            <p>this is the red box</p>
        </div>
    </div>
</div>

Hope this helps!

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.