JQuery context

I am trying to make the following choice:

$(".program", row) 

Where "row" is a jQuery object containing two rows of a table. One of tr has a class program. This selector does not seem to find it. However, the following works:

 $(".title", row) 

where div.title is a descendant of tr.program.

If I use a jQuery object as a selector context, can I match the top level elements of this jQuery object?

thanks,

-Morgan

+6
javascript jquery
source share
4 answers

It looks like you are trying to select elements from those that you have already selected (living in a jQuery object).

The context, in relation to jQuery, is similar to specifying the parent - the node context is ABOVE somewhere that you are looking for in the DOM tree. Context is where jQuery will look for the selector you specify.

If I'm right, what are you trying to do, this should work:

 row.filter('.program'); // And then: row.filter('.program').find('.title'); 
+6
source share

My understanding is that the context should be the root level, that is, the parent in which you want to make your choice.

EDIT: After doing some reading, you should be able to map top-level context elements (the default context is document ).

Beardscratchers has a good article on using context with jQuery selectors. In general, you should try to pass the identifier of the element as the context for the jQuery wrapped set, as this is the most efficient way to locate the element.

Internally, the selector context is implemented using the .find method, so $(selector, context) equivalent to $(context).find(selector)

+2
source share

You are on the right track with your descendants. Passing context to the selector searches for all descendants found in this context.

In your case, since the object is not a "parent containing two lines", but rather a "set of lines", the program cannot be selected. Since .title is the actual descendant in your collection, you can select it.

0
source share

You can certainly match top-level items. Can we see the source you are using? Your code should work, so perhaps the problem is in the HTML itself.

-one
source share

All Articles