Skip to content

Commit

Permalink
Merge pull request #3 from rarcega/master
Browse files Browse the repository at this point in the history
Added support for <select> elements with multiple attribute
  • Loading branch information
spencertipping committed Apr 10, 2014
2 parents e82d836 + deb082d commit 48e117e
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions jquery.fix.clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// jQuery's clone() method works in most cases, but it fails to copy the value of textareas and select elements. This patch replaces jQuery's clone() method with a wrapper that fills in the
// values after the fact.

// An interesting error case submitted by Piotr Przybył: If two <select> options had the same value, the clone() method would select the wrong one in the cloned box. The fix, suggested by Piotr
// An interesting error case submitted by Piotr Przybyl: If two <select> options had the same value, the clone() method would select the wrong one in the cloned box. The fix, suggested by Piotr
// and implemented here, is to use the selectedIndex property on the <select> box itself rather than relying on jQuery's value-based val().

(function (original) {
Expand All @@ -17,8 +17,13 @@
result_selects = result.find('select').add(result.filter('select'));

for (var i = 0, l = my_textareas.length; i < l; ++i) $(result_textareas[i]).val($(my_textareas[i]).val());
for (var i = 0, l = my_selects.length; i < l; ++i) result_selects[i].selectedIndex = my_selects[i].selectedIndex;

for (var i = 0, l = my_selects.length; i < l; ++i) {
for (var j = 0, m = my_selects[i].options.length; j < m; ++j) {
if (my_selects[i].options[j].selected === true) {
result_selects[i].options[j].selected = true;
}
}
}
return result;
};
}) (jQuery.fn.clone);
Expand Down

0 comments on commit 48e117e

Please sign in to comment.