Writing if/else statements in the middle of jQuery code doesn't feel right:
if ($("#test").length) {
// do smth
}
else {
// do smth else
}
Is there something similar to:
$("#test").test(function(){
// do smth
},
function(){
// do smth else
});
Writing if/else statements in the middle of jQuery code doesn't feel right:
if ($("#test").length) {
// do smth
}
else {
// do smth else
}
Is there something similar to:
$("#test").test(function(){
// do smth
},
function(){
// do smth else
});
Share
Improve this question
asked Apr 25, 2012 at 14:43
julien_cjulien_c
5,1127 gold badges42 silver badges55 bronze badges
4
- 3 It depends on what you are doing. What are you doing? – LeonardChallis Commented Apr 25, 2012 at 14:45
- You could always do <code>$("#test").length ? (function() { })() : (function() { })();</code> – slowpoison Commented Apr 25, 2012 at 14:45
- 9 jQuery code is just normal code - if writing 'if / else' bothers you, perhaps you shouldn't be writing code... – Alnitak Commented Apr 25, 2012 at 14:46
-
1
That's actually more typing and reading, and adds indentation unless you indent inconsistently (or follow a silly coding style). Why wouldn't you just use
if ... else
? – user395760 Commented Apr 25, 2012 at 14:48
4 Answers
Reset to default 9Is there something similar to:
There is, now!
(function($) {
$.fn.test = function(a, b) {
(this.length ? a : b).apply(this);
return this;
};
})(jQuery);
Working demo at http://jsfiddle/alnitak/n2CLZ/
as far as I know no... but you can use ternary operators for short conditions
example:
var foo = $("#test").length ? "bar" : "baz";
EDIT: when using functions
$("#test").length ? (function() { console.log("#test available"); })() : (function() { console.log("#test not available"); })();
Things to note:
an if/else statement is:
if(condition)
{
//something
} else {
//something
}
whereas, a switch is:
switch(someVal) {
case 'valEqualsThis':
//do something
break;
}
a short version of if else is the single line syntax:
if(someCondition)
//dosomething
else
//dosomethingelse
an even shorter version of that is the ternary operator
someoutput = (someCondition) ? doTrue : doFalse
Jquery hasn't really gotten into the business (yet) of trying to further simplify these already short ways of checking conditions, but since jquery is extendable like any other javascript, if you need a jquery native function, just write one into the $
namespace
The example you used wouldn't be needed at all since if no elements are selected, the methods called don't do anything.
I can't think of a situation where it would make sense to have an if statement in the middle of a jQuery chain other than inside of something such as a filter method or an each method.
Update
I'm looking at this from a useability point of view. Compare these two situations:
if ( $(".div").length ) {
console.log("We found divs");
} else {
console.log("We did not find div");
}
vs
$(".div").test(function(){
console.log("We found divs");
},function(){
console.log("We did not find divs");
});
Maybe the jQuery method is easier to read for you, to me it's about the same. The part that kills it for me is the fact that the jQuery method can only check the length unless you add in a boolean parameter and more logic inside the plugin, while with the if statement you simply change the conditional.
If the plugin accepted a conditional, it would end up doing this.each
and checking the condition on each element, however the conditional that you pass in would have to be a function that returns true
or false
for the given element. Kinda like the already existing .filter
method.
Update
For pleteness, here's a fiddle paring the plugin that accepts a condition function to using $.fn.each
. http://jsfiddle/fdYSx/2/
I don't think it's any more readable or easier to use.