0

Just wonder this for a long time. I have store a variable like, var sth = $("#mydiv ul li"), so I am able to do something like

sth.each(function(){
//
});

now I'd like to add an extra a tag at the end, like $("#mydiv ul li a") how do apply a to sth

should like $(sth + 'a')?

4 Answers 4

4

EDIT 2: sorry I was right in fact... $.add(); to add a selector

var sth = $('ul li');
sth.add('a').each(function () {

});
// === $('ul li, a');

Or if you prefer to find a new selectors in the children of sth.

sth.find('a').each(function () {

});
// === $('ul li a');
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you add is the word :-)
I discovered it today too @MarcelGwerder !
Now what would select all LI elements and anchors ?
$('ul li').add('a') would give you the same as $('ul li, a'), not $('ul li a')
@olo In that case you may not have picked the right example in your question, as $('#mydiv ul li').add('a') will include all <a> tags on the page, where $("#mydiv ul li a") would not.
|
1
$('a', sth).each(function(){
   ...
});

finds all anchors inside sth.

5 Comments

The disadvantage of that syntax over .find() is that you have to break the chain. e.g. you can't easily do the equivalent of sth.show().find('a').hide()
The drawback is that it requires you to rearrange the expression to put your previous result in the middle, breaking the common jQuery "chaining" idiom, where code always reads forwards, as a continuous chain of operation().operation().operation() ...
The sth.show() is the first operation to run, but is written in the middle of the expression. For a simple case like this, it's not much different, but as complexity grows, chaining is a key feature of jQuery which makes for much more readable code. Consider just one more level of nesting: $('.external', $('a', sth.show()).addClass('magic')).hide() vs sth.show().find('a').addClass('magic').find('.external').hide()
I do this all the time, using ternary expressions, filter, grep, map and even appends ......: $(elem).append( elem2.append( elem3.prepend(anchor.find('a')) ) ) etc. makes sense to me ?
Matter of taste I guess, but to me that is a lot harder to read than the chaining-based equivalent, as I have to track back and forth to see where the brackets begin and end, and find the middle of the nest just to see what the original input was. It also just doesn't feel like jQuery, because chaining is such a common idiom in basically every example and plugin.
1

If I understand correctly and you want to targert the a descendants of sth, you should try:

sth.find('a').each(function(){

That would be the equivalent of $("#mydiv ul li a")

Comments

1

I think what you're looking for is .find().

As the documentation puts it, it "Searches for descendent elements that match the specified expression." In most cases, this is the same as adding an extra word to your CSS selector.

So in your example you can do sth.find('a').each( ... )

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.