Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we have jquery selector using methods like $.id('header').childs('link').whenAttr('extra').all('a')

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;
like image 320
Roc Ho Avatar asked Dec 21 '25 07:12

Roc Ho


1 Answers

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.
    }
}
like image 52
Arindam Avatar answered Dec 24 '25 12:12

Arindam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!