Sometime I need to dynamically build a query selector string.But by using strings it looks ugly
$('#header>link[' + extraAttr + '] ' + childElement);
Nearly unreadable,hard to maintain.
So I wondering if there have any method-query api just let us to build a query like linq in .net
query = query.Where(xxx);
if(xxx){
query.First(xxx);
}
return query;
You can use variable names instead of hard-coding the values in the selector class names.
For especially your case, this could be a possible choice.
var children = $('#header').children('link'); // get the link children of header
var aElements = []; // create an empty array for expected a elements
$.each(children) function(index, obj) { // operate on each of the link elements
if(($(obj).attr('extra')).length > 0) { // if the element has the attribute extra
aElements.push($(obj.attr('class') + " a")); // push the a elements in its child to the array. Eventually the array will contain the list of all the desired 'a' elements.
}
}
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