3

I'm practicing conditional logic within certain JQuery functions. $('h2').css({backgroundColor: 'red'}); works, but when I add the conditional logic below it no longer works. If I change the return value to a string it will get rid of the error, but it still doesn't change the background color. What am I doing wrong?

$('h2').css({function(){
    if (1 === 1){
        return backgroundColor: 'red';
    }
    else {
        return backgroundColor: 'purple';
    }
}});
2
  • Generally, you cant return multiple things, and you can only return values (whole strings, numbers, objects...) . This might work but only if you format the return value to return something like "baclgroundColor : 'purple' " (note the quotes) Commented Jun 26, 2015 at 16:53
  • I didnt phrase this correclty when I said "multiple things", but there are good answers Commented Jun 26, 2015 at 16:55

4 Answers 4

11

Your example code will cause a syntax error. You can use callback functions for jQuery.css in one of two ways.

An object with properties and their callback functions.

$('h2').css({
    backgroundColor: function(){
        if (1 === 1){
            return 'red';
        }
        else {
            return 'purple';
        }
    }
});

Or with a single property and callback function pair.

$('h2').css('backgroundColor', function(){
    if (1 === 1){
        return 'red';
    }
    else {
        return 'purple';
    }
});

In this way, you can conditionally apply CSS per-element.

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

Comments

10

Try this code:

$('h2').css({backgroundColor: (1 === 1) ? 'red' : 'purple'});

It's the shorthand of if condition.

In this case, the shorthand of if condition is more reliable and cleaner.

By the way, it's a plain old javascript.

Hope it helps.

7 Comments

I'm sure this will make it work, but it would be more useful to the OP to first explain what's wrong with his code.
I'm new to JQuery and had no idea you could write it this way. Very interesting. Thank you.
@Bryan Thanks. I just discovered this one with jquery and angularjs with my project. This is not necessarily for jquery usage only. It's a plain old javascript. I'm glad it helps.
There is no such thing as "jQuery usage only." jQuery is a JavaScript library; there's nothing you can do with it that you couldn't do with plain JavaScript.
@Juhana I'm referring about the shorthand of if condition not about the jquery codes.
|
5

Fascinating thought experiment! The issue is that you are passing a function reference to the .css function which accepts strings and objects only. Try encapsulating the function so it returns the return result rather than the function reference:

$('h2').css((function(){
  if (1 === 1){
    return {backgroundColor: 'red'};
  }
  else {
    return {backgroundColor: 'purple'};
  }
})());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Cool thought experiment</h2>

Comments

-3

Is more cleaner keep your style in the style sheet file and is more cleaner to use the toggleClass handler

.highlight{//Put this in your style sheet file
    background-color : red;
}

'//===>This will add the to the element the highlight if it doesn't have it or else will returned back to the original style

$('h2').on('click',function(){
    $(this)toggleClass('highlight');
})

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.