Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would $('div, span', $('.test')).selector output? jQuery bug?

I have something similar to the following selector:

$('div, span', $('.test')).selector

I expect the selector that is produced to be:

.test div, .test span

But its actually produces

.test div, span

What gives? I need the result to as expected otherwise its a huge headache.

like image 384
anthonyv Avatar asked Mar 04 '26 21:03

anthonyv


1 Answers

Is it a bug? Well...it's debatable. It's not reliable in these situations (and many others), but per the jQuery API, it doesn't have to be.

.selector though accessible, isn't a supported or docunmented part of the API, so it may do some strange and unsupported things.

The short version: it won't so what you're after here, because it's designed for much simpler situations (usually one level deep), like .live() use internally.


Edit: If anyone's curious as to the why part of this, it's just how it's written, what you have: $('div, span', $('.test')) is actually doing a $('.test').find('div, span') which calls .pushStack() underneath, which is very simple: only appending a space and the new selector.

like image 94
Nick Craver Avatar answered Mar 06 '26 11:03

Nick Craver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!