-
Notifications
You must be signed in to change notification settings - Fork 483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explain how to test if element attached to main DOM #725
base: main
Are you sure you want to change the base?
Conversation
Thank you for your pull request. It looks like this may be your first contribution to a jQuery Foundation project, if so we need you to sign our Contributor License Agreement (CLA). 📝 Please visit http://contribute.jquery.org/CLA/ to sign. After you signed, the PR is checked again automatically after a minute. If there's still an issue, please reply here to let us know. If you've already signed our CLA, it's possible your git author information doesn't match your CLA signature (both your name and email have to match), for more information, check the status of your CLA check. |
Thank you for the PR, you made a good point. I have a few suggestions to improve it. I'd move this paragraph as the first of your PR to explain the potential issue:
|
I was thinking that it might worth to show an example.
var span = $( "span" );
var fragment = document.createDocumentFragment();
console.log( span.length ); // 1
fragment.appendChild( span.get( 0 ) );
console.log( span.length ); // Still 1!
console.log( $( "span" ).length ); // 0. The element was detached Any thoughts? |
@AurelioDeRosa it may confuse if you don't use $('body').html('<div><a></a></div>');
var element = $('body a');
console.log(element.length); // 1
console.log(element.parents().length); // 3 div, body, html
$('body').html('');
console.log(element.length); // 1
console.log(element.parents().length); // 1 div |
Hi @Enelar. I'm OK with your example too as far as we have one to clarify the concept. |
Since DocumentFragment era
.length
do not provide accurate answer to that question in several scenarios.