3

In the following code, after the jQuery runs, I expected only 2 to be seen with background red.

However, after jQuery runs, I can see 1, 2 and 3 with their backgrounds red.

Where is the glitch?

HTML

<html>
<head> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> 
    <script>
        $(document).ready(function(){
            $('div').css('display','block').css('background','red');
        });
    </script> 
</head> 
<body> 
    <div style="display:none">1</div>
    <div style="display:block">2</div> 
    <div style="display:none">3</div> 
</body> 
</html> 

Output

enter image description here

1
  • What is the requirement? Commented Sep 21, 2016 at 9:31

6 Answers 6

3

.css('display','block') is not a selector, it's a setter. You set display: block to all divs and then set a red background to all too. It's called a chain.

$('div')     .css('display','block').css('background','red');
//^ selector  ^ first setter         ^ second setter

There are plenty of ways to select the correct div.

$("div:visible").css({display: "block", background: "red"});
$("div").filter(":visible").css({display: "block", background: "red"});
$("div[style='display:block']").css({display: "block", background: "red"});
$("div:eq(1)").css("display", "block").css({display: "block", background: "red"});
$("div").eq(1).css("display", "block").css({display: "block", background: "red"});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div style="display:none">1</div>
<div style="display:block">2</div>
<div style="display:none">3</div>

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

Comments

2

Your jquery is working correct, you have first set all divs to be block and then coloured them red.

If you only want to select the visible div to colour, try using the selector :visible

$(document).ready(function() {
  $('div:visible').css('background', 'red');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<div style="display:none">1</div>
<div style="display:block">2</div>
<div style="display:none">3</div>

Comments

0

The jquery code is overriding the inline style. Set which divs to be displayed from the script.

Comments

0

Remove the .css('display','block') :

<html>
<head> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> 
    <script>
        $(document).ready(function(){
            $('div:visible').css('background': 'red');
        });
    </script> 
</head> 
<body> 
    <div style="display:none">1</div>
    <div style="display:block">2</div> 
    <div style="display:none">3</div> 
</body> 
</html> 

Comments

0

You are selecting them all and then setting the display as block with background red I think something like this

$('div:visible').css('background','red');

Comments

0

If you expect only 2 to be seen with background red, Please try this.

<html>
<head> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> 
    <script>
        $(document).ready(function(){
            $('div').each(function(){    
                if ( $(this).css('display') == 'block')
                {
                    $(this).css('background', 'red');
                }
            });
        });
    </script> 
</head> 
<body> 

3 Comments

Really, don't use each for such tasks!
Thank you for your suggestion. could you edit the html code without using each function for me?
Just take a look at my answer.

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.