1

I'm working on a project in jQuery and I'm selecting multiple CSS to change on a condition:

if (jQuery('#1').is(":visible")) {
      jQuery('#2').css({"border-top":"0px", "border-bottom":"7px solid #0767B1"});    
      jQuery('#3').css({"display:table-cell"}); 
}                

It works with only the #2 css change, but when I add #3 it isn't working anymore.

5
  • 1,2 and 3 are not valid id's. They must not start with a digit. Commented Jun 26, 2013 at 9:44
  • Check your ". "display:table-cell" should be "display":"table-cell" Commented Jun 26, 2013 at 9:44
  • 1
    @Fabrizio try for html4.01, wrong for html5. Commented Jun 26, 2013 at 9:46
  • Just a side note: IDs and classes cannot start with numbers. Just like variables in JavaScript. Commented Jun 26, 2013 at 10:56
  • @Fabrizio Calderan & Ahmad Alfy, I know. It was just for this example, changed the names to those numbers. Commented Jun 26, 2013 at 11:54

3 Answers 3

3

Put the : out of the quotes:

jQuery('#3').css({                        //this is used for multiple values
    "display":"table-cell"
}); 

Or like:

jQuery('#3').css("display","table-cell"); //for single values 


More at Jquery .css() syntax

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

Comments

3

Try it like this:

if (jQuery('#1').is(":visible")) {
      jQuery('#2').css({"border-top":"0px", "border-bottom":"7px solid #0767B1"});    
      jQuery('#3').css("display", "table-cell"); 
} 

The jQuery .css() method can be used in two ways. You can manipulate a single attribute, which looks like .css("attribute", "value");, or you can manipulate multiple values, but that has a different writing: .css({"attribute" : "value", "attribute2" : "value"});. But you can use the second method (as you tried) also for just a single attribute, I didn't notice your small mistake but as Vucko mentioned you just forgot to surround the : with ", so this solution was what you were looking for (but it's usually for multiple arguments as you use the { } brackets):

jQuery('#3').css({"display":"table-cell"}); 

And my recommendation was to use the single attribute notation:

jQuery('#3').css("display", "table-cell");

4 Comments

Explaining is always better than just throwing a block of code at the reader and playing the "find the character I changed" game.
@Francodi Can you explain what is difference between your answer and "Pim Peters" Question.
Please add an explanation, then I happily convert my -1 into a +1.
You're right, I just thought he would see the changed line difference and yeah... that was not really helpful. Will do it better the next time I response to someone, thanks for telling.
0

You can use below code for css change using jquery

// For single values :
if($('#example').css('background-color','yellow')) {
    $('#1').css("display","none");
}

// For Multiple values :
if($('#example').css('background-color','yellow')) {    // Condition code
    $('#1').css("display","none");
    $('#2').css({
      'background-color' : 'yellow',
      'font-weight' : 'bolder'
    });
}

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.