0

Ok anybody familiar with building your own custom jQuery selection from DOM nodes or jQuery objects?

For examle:

var li = jQuery('li.someclass');
var myDiv = jQuery('#mydiv');

So I would like to combine those two into one jQuery(object) .

It would be nice if it can be done dynamically using nodes cached in variables like the provided example. BR and 10x for your kind help

2 Answers 2

3

You can combine the results with .add().

var combined = li.add(myDiv);

Note that .add() doesn't really do what it claims. It doesn't add anything to the li jQuery object. It creates a new jQuery object with the merged results.

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

Comments

1

I would do the following in this case:

var obj = jQuery('#mydiv, li.someclass');

Then any method performed on obj would apply to both #mydiv and li.someclass. For example, to give a red border to both elements:

obj.css('border', '1px solid red');

1 Comment

Furthermore, jQuery uses the Sizzle selection engine, which supports any CSS3-style selection.

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.