Skip to content

.clone() documentation is incorrect #381

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

Closed
ithcy opened this issue Nov 5, 2013 · 4 comments
Closed

.clone() documentation is incorrect #381

ithcy opened this issue Nov 5, 2013 · 4 comments

Comments

@ithcy
Copy link

ithcy commented Nov 5, 2013

As noted in this stackoverflow question:

The API for clone() says:

The .clone() method performs a deep copy of the set of matched
elements, meaning that it copies the matched elements as well as all
of their descendant elements and text nodes. For performance reasons,
the dynamic state of form elements (e.g., user data typed into input, and textarea or user selections made to a select) is not copied
to the cloned elements.
The clone operation sets these fields to
their default values as specified in the HTML.

The docs appear to be wrong. User data typed into inputs is indeed copied to the cloned elements. I verified this with several tests. I could not get .clone() to not copy the value of a text input that whose value was empty in the HTML. The value of the cloned element must be explicitly cleared after cloning if this is the desired result.

ithcy pushed a commit to ithcy/api.jquery.com that referenced this issue Nov 5, 2013
@dmethvin
Copy link
Member

dmethvin commented Nov 6, 2013

If this is now happening I am thinking it might be because the browsers changed their behavior. Do you have a test case showing the behavior for all inputs including things like textarea as well? Does it happen consistently in all browsers?

@ithcy
Copy link
Author

ithcy commented Nov 6, 2013

Here's a test case: http://jsbin.com/OKExOvu/1/edit?html,js,output

It only happens with text inputs and checkboxes. Textareas and selects do not retain user input/selection. So my pull request should probably change to reflect that.

It happens in Chrome, Firefox and IE as far back as IE8 (IE7 not tested). So it's not a browser change.
It happens with all versions of jQuery from 2.x-git all the way back through 1.2.3. (via google CDN). So it's not a new jQuery behavior.

Since this appears to be so embedded, I'm thinking it's probably better to change the documentation than try to patch jQuery, since there may be applications depending on this behavior.

There are also several other questions on stackoverflow referencing this issue. The answers all recommend clearing the value of the new element after cloning.

@dmethvin
Copy link
Member

dmethvin commented Nov 6, 2013

Thanks for doing the legwork on this @ithcy. I did seem to remember that textarea didn't retain its data on a clone. The only place where we special-case inside jQuery is with check boxes, where the behavior wasn't consistent across browsers.

If you could update the pull request to mention the exceptions you found, that would be great!

@ithcy
Copy link
Author

ithcy commented Nov 6, 2013

No problem! Test case updated to include checkboxes, which do appear to retain their [un]checked state when cloned. I will update the pull request shortly.

ithcy pushed a commit to ithcy/api.jquery.com that referenced this issue Nov 7, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants