Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  145] [ 1]  / answers: 1 / hits: 21357  / 12 Years ago, fri, april 13, 2012, 12:00:00

Actually two questions:



I like the shorthand IF-ELSE in jQuery like so:



var some == true ? it's true : it's false;   


Is there a corresponding IF-THEN, because this:



   var some == true ? it's true 


doesn't seem to work.



Also, would something like this be chainable in regular jQuery? Like so:



  some.addClass(hello)
.find(.other).css(width,100px).end()
// chained if
.hasClass(one) ? some.css(border,1px solid red)
.removeClass(hello)


Now that would be nice, wouldn't it? Is this possible?


More From » jquery

 Answers
30

There are a couple of plug-ins that allow you to chain if logic in jQuery -



http://outwestmedia.com/jquery-plugins/conditional/



$('div')
.If('hasClass', 'myClass')
.css('color', 'red')
.Else('hasClass', 'anotherClass')
.css('color', 'green')
.Else()
.css('color', 'blue');


http://benalman.com/projects/jquery-iff-plugin/



function my_test( x ) {
return x === 'bar';
};

$('div')
.append( '1' )
.iff( my_test, 'foo' )
.append( '2' )
.end()
.append( '3' );

[#86257] Thursday, April 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
taliac

Total Points: 84
Total Questions: 114
Total Answers: 114

Location: Morocco
Member since Fri, May 22, 2020
4 Years ago
taliac questions
Sun, Mar 21, 21, 00:00, 3 Years ago
Tue, May 12, 20, 00:00, 4 Years ago
Mon, Jan 13, 20, 00:00, 4 Years ago
;