LCJ
LCJ

Reputation: 22652

Using Multiple Selectors in jQuery

I have a working jQuery statement as follows

 var firstHeaderLineElement = $(".resultGridTable .tableColGroupAssociate");

I need to make this more generalized by making .tableColGroupAssociate as a variable. I have achieved this using following:

    var hideClass = '.tableColGroupAssociate';
    var firstHeaderLineElement = $(".resultGridTable").find(hideClass);

However, it requires a "find". Is there a better performing jQuery way for this?

Upvotes: 2

Views: 63

Answers (2)

thecodeparadox
thecodeparadox

Reputation: 87073

Using String concatenation:

var hideClass = '.tableColGroupAssociate';
var firstHeaderLineElement = $(".resultGridTable " + hideClass );

jQuery $(selector, context) format:

var hideClass = '.tableColGroupAssociate';
var firstHeaderLineElement = $(".resultGridTable", hideClass);

But internally it implements the .find().


Note

But jQuery always not implements the find() method. In modern browsers it try to implement the document.querySelectorAll() so that browser will try to parse it as a valid CSS selector.

If this default engine fails then jQuery parse the selector using its default engine Sizzle which using its internal mechanism for DOM traversing.

Upvotes: 3

Tom
Tom

Reputation: 3040

you can use multiple strings in selector identifying

var hideClass = '.tableColGroupAssociate';
var firstHeaderLineElement = $(".resultGridTable " + hideClass);

Upvotes: 3

Related Questions