I need to return True
if a page contains any of a list of element types. This code works.
var elements = 0;
var content = $('.TestModule');
if (content !== null && typeof (content) !== "undefined") {
elements += content.first().find('.btn').length;
elements += content.first().find('.textbox').length;
elements += content.first().find('.ui-jqgrid').length;
elements += content.first().find('.flexboxdiv').length;
elements += content.first().find('.headerlabel').length;
}
return elements > 0;
But always runs every test which is obviously inefficient so I changed to;
var valid = false;
var content = $('.TestModule');
if (content !== null && typeof (content) !== "undefined") {
var elementTypes = ['btn', 'textbox', 'ui-jqgrid', 'flexboxdiv', 'headerlabel'];
for (i = 0; i < elementTypes.length; i++) {
var num = content.first().find('.' + elementTypes[i]).length;
if (num > 0) {
valid = true;
break;
}
}
}
return valid;
Which also works and runs the minimum number of tests. But it looks pretty clunky to me. Can you suggest a better loop/structure/test, please? I don't need a count of element types, just to know whether one or more of any type exists.