23

What is a simple jQuery statement that states an operation proceeds only if A and B are true? If A isn't true, stop. If A and B are true, then continue. `

6
  • 3
    As asked, this seems a general javascript question -- can you please clarify what's jquery-specific about it? Commented May 20, 2009 at 1:22
  • 2
    I hope you realize that jquery is a library, and the language still is javascript. Commented May 20, 2009 at 5:19
  • 3
    Yes, harshath.jr, I realize that. Commented May 21, 2009 at 15:46
  • 2
    I'm assuming that the OP is looking for something that would lead to a jquery construct like this: $(selector).DoIf(function(){return A && B})// continue jquery chaining but actions only happen if the DoIf predicate succeeds Commented Mar 16, 2012 at 11:26
  • 1
    in which case $(selector).filter(function(index)) can do what he's looking for. Commented Mar 16, 2012 at 11:34

7 Answers 7

87

jQuery is just a library which enhances the capabilities of the DOM within a web browser; the underlying language is JavaScript, which has, as you might hope to expect from a programming language, the ability to perform conditional logic, i.e.

if( condition ) {
    // do something
}

Testing two conditions is straightforward, too:

if( A && B ) {
    // do something
}

Dear God, I hope this isn't a troll...

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

Comments

10

You can wrap jQuery calls inside normal JavaScript code. So, for example:

$(document).ready(function() {
    if (someCondition && someOtherCondition) {
        // Make some jQuery call.
    }
});

Comments

3

To add to what the others are saying, A and B can be function calls as well that return boolean values. If A returns false then B would never be called.

if (A() && B()) {
    // if A() returns false then B() is never called...
}

1 Comment

As long as A() doesn't return false or null it will be true.
2

It depends on what you mean by stop. If it's in a function that can return void then:

if(a && b) {
    // do something
}else{
    // "stop"
    return;
}

Comments

1
if(A && B){ }

Comments

0

I little sophisticated way:

$(selector).is(condition)? alert('true') : alert('false');

Ex:

$("#btn-primary").is(":disabled")? alert('button disabled') :  alert('button disabled');

Comments

-1

If you're using Jquery to manipulate the DOM, then I have found the following a good way to include logic in a Jquery statement:

$(selector).addClass(A && B?'classIfTrue':'');

1 Comment

This answer is deviant to the question asked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.