I’ve noticed that the .end() cannot get back the old set of jquery elements, if I set jQuery elements in find().
According to jquery document http://api.jquery.com/find/ . It should accept jQuery object. Is it a bug? Or something I should know ? Thank you very much.
<section>
<div id="myDiv" style="display:none;">
This is my DIV ... <span id="p-text"></span>
</div>
</section>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
window.onload = function(){
var $m = $('#myDiv');
var $t = $('#p-text');
// this will show myDiv
$m.find('#p-text').text('blabla').end().show();
// this will not show myDiv
$m.find($t).text('blabla2').end().show();
}
</script>
Re-EDIT:
The difference is between
$m.find(<string selector>)and$m.find(<jquery object>);Assumptions
end()function return an object calledprevObject(this is a not-documented public property of jquery stack), stored in returned jquery stack.findandfilterfindbehaves differently depending on type of argument (stringorjquery Object)what happens
<String selector>then it store the current jquery object in thisprevObjectproperty.<jquery object>then store the RETURNED stack in this property.indeed:
and it works because
$mis the element withdisplay:nonein the second case:
and it does NOT works because the
$telement is only a child of hidden element.Why?
The
findmethod use internally the methodjQuery( selector ).filterif theselectoris a jquery object. Here the piece of source codeThis call return a stack with prevObject filled with
jQuery( selector )instead of original stack. In your case the prevObject is the jquery object$tinstead of$m.I think that’s a bug resulted from a objectless use of jquery library (your code has no sense because you should write simply
$t.text('blabla2').show())