Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chaining selectors in jQuery

I'm a guy used to mootools' way of chaining selectors, and I can't seem to find anywhere how to do the same in jQuery. Suppose I have a select element in the selectObj variable. What I need is to get the last option in that select. In mootools I would have done something like:

var option = $(selectObj).getElement('nth-child(last)') 

Can I do something similar, or what is the way of getting that last option in jQuery?

PS. I know about the parent > child selector, but I can't really use it because I don't know what selector has been used to get the select. I only have the resulting element.

like image 272
andi Avatar asked Mar 31 '09 12:03

andi


People also ask

Can you chain jQuery selectors?

jQuery Method Chaining However, there is a technique called chaining, that allows us to run multiple jQuery commands, one after the other, on the same element(s). Tip: This way, browsers do not have to find the same element(s) more than once. To chain an action, you simply append the action to the previous action.

What is jQuery chaining?

jQuery | chaining() With jQuery, we can use do chaining which means to chain together multiple methods in a single statement on a single element. We have been using a single statement at once, but now using the chaining method we can bind multiple methods to make the code short.

Can we use multiple selectors in jQuery?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.


1 Answers

$(selectObj).find(':last') 

You can use find to perform another query within the current query.

In general, you can check out the Selectors and Traversal pages on jQuery docs when you're trying to figure out how to select something.

like image 78
bdukes Avatar answered Sep 23 '22 03:09

bdukes