question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jQuery .val() method on a cloned form element doesn't update its value attribute

See original GitHub issue

I had a weird problem using zombie.js for a functional test of some production cloning script. It seems that using jQuery’s val() on a cloned, unattached element doesn’t work as it would in-browser. Here’s a stripped-down scenario to reproduce it:

<html><body>
<form>
    <input type="text" id="sourceField" name="field" value="should not expose this">
</form>

<script>
(function($) {
    var $template = $('#sourceField').clone();
    $template.val('');
    var $new = $template.clone();
    // in production code, would rename and append here
    console.log($new.prop('name') + ': ' + $new.val());
})(jQuery);
</script>
</body></html>

Running this in a browser console, you see:

field:

In zombie.js, the console shows:

field: should not expose this

If I replace the $template.val(''); with $template.attr('value', '');then the code works consistently in both. It seems that the .val() method doesn’t update the value attribute of the cloned node, which allows its content to persist across further clone operations.

I see it using jQuery 1.8 through 1.11.1. Looking at the jQuery code it seems that it should be a pretty straightforward assignment. I’d welcome someone with better knowledge of jsdom taking a quick look to see if it’s an obvious bug in how it’s handled.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:15 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
matthewpcommented, Jul 2, 2016

I assume you mean this part:

The cloning steps for input elements must propagate the value, dirty value flag, checkedness, and dirty checkedness flag from the node being cloned to the copy.

Will do! Going to see if there are any WPT for this, otherwise will create a new one in to-upstream.

0reactions
domeniccommented, Jul 5, 2016
Read more comments on GitHub >

github_iconTop Results From Across the Web

jquery - Clone isn't cloning select values - Stack Overflow
jQuery's clone() method works in most cases, but it fails to copy ... Cloning a <select> does not copy the value= property on...
Read more >
clone to copy form field values - jQuery Forum
This code snippet, when added to the clone() method copies form field values. Tested with IE 7 and FF 2 on Windows.
Read more >
jQuery val() Method - W3Schools
This method sets the value of the value attribute for ALL matched elements. Note: The val() method is mostly used with HTML form...
Read more >
clone problem - Forums - CSS-Tricks
Although I don't know for sure what's wrong, there is a problem in your code, namely the ID attributes on the elements that...
Read more >
Cloning rows, but.... - Laracasts
clone (); // Update name attribute of all input elements in the group: $('[name]', clone).each(function(){ var $this = $(this); clone.find("input ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found