2

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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Does String concatenation also use find() internally?
3

you can use multiple strings in selector identifying

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.