In python, I can do the following, to conditionally use a 2nd list if a 1st one is empty:
>>> x = [ ] or [1, 2]
>>> x
[1, 2]
In javascript, however,
>>> x = [ ] || [1, 2];
[ ]
So, if the "or trick" is off the table, I'm wondering whats the most concise way to do something like the following if-javascript-were-python-this-would-work pseudocode:
$elems = $('first-selector') || $('second-selector')
var elems = $("first-selector");
if(!elems.length) {
elems = $("second-selector");
}
is not concise enough?
How about writing a little jquery extension?
$.fn.or = function(selector) {
if (this.length) {
return this;
}
return $(selector);
}
var elems = $("first-selector").or("second-selector")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With