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. `
-
3As asked, this seems a general javascript question -- can you please clarify what's jquery-specific about it?Alex Martelli– Alex Martelli2009-05-20 01:22:46 +00:00Commented May 20, 2009 at 1:22
-
2I hope you realize that jquery is a library, and the language still is javascript.jrharshath– jrharshath2009-05-20 05:19:41 +00:00Commented May 20, 2009 at 5:19
-
3Yes, harshath.jr, I realize that.Kevin Brown– Kevin Brown2009-05-21 15:46:57 +00:00Commented May 21, 2009 at 15:46
-
2I'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 succeedsstevenrcfox– stevenrcfox2012-03-16 11:26:40 +00:00Commented Mar 16, 2012 at 11:26
-
1in which case $(selector).filter(function(index)) can do what he's looking for.stevenrcfox– stevenrcfox2012-03-16 11:34:42 +00:00Commented Mar 16, 2012 at 11:34
7 Answers
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...
Comments
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
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':'');