0

I use the same element in my function several times. For example:

var myElement = jQuery('#some_div_with_good_id');
var myChildren = jQuery('#some_div_with_good_id span');

So how I can optimize this code for shorten second variable? I tried:

var myChildren = jQuery(myElement, 'span');

but this not works :( I'm new in jQuery, so sorry if you think it is stupid question.

2
  • var myChildren = myElement.find('span'); Commented Apr 7, 2013 at 22:31
  • It's the other way around -> jQuery('span', myElement); Commented Apr 7, 2013 at 22:32

2 Answers 2

3

Try it the other way, jQuery('span', myElement) should work.

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

1 Comment

Thank you Steven! The decision was so close :)
0
var children = $( element ).children( 'span' );

2 Comments

That's not equivalent to jQuery('#some_div_with_good_id span'), which finds descendant spans at all levels; .children() just finds immediate children.
Ah. Going by his variable name (myChildren), I figured he only wanted the children of that element.

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.