jQuery always returns elements in DOM order, so you need to do something on these lines:
$.each(['.selector1', '.selector2', '.selector3'], function(i, selector) {
var res = $(selector);
if (res.length) {
res.first().click();
return false;
}
});
You could use this in a jQuery extension, for example:
$.coalesce = function(selectors) {
var match;
var selector = $.each(selectors, function(i, selector) {
var res = $(selector);
if (res.length) {
match = res;
return false;
}
});
return match || $([]);
};
And then call
$. coalesce (['. selector1', '.selector2', ".selector3 ']) first () click (); ..
source
share