diff --git a/README.md b/README.md
index 7529c6f..fb7f671 100644
--- a/README.md
+++ b/README.md
@@ -57,7 +57,7 @@ A string or jQuery object to prepend or append to the printThis iframe content.
$('#mySelector').printThis({
header: "
Amazing header
"
});
-
+
$('#mySelector').printThis({
footer: $('.hidden-print-header-content')
});
@@ -69,15 +69,13 @@ As of 1.9.1, jQuery objects will be cloned rather than moved.
The `base` option allows several behaviors.
By default it is `false`, meaning a the current document will be set as the base URL.
-If set to `true`, a `` attribute will be set if one exists on the page.
+If set to `true`, a `` attribute will be set if one exists on the page.
If none is found, the tag is omitted, which may be suitable for pages with Fully Qualified URLs.
When passed as a string, it will be used as the `href` attribute of a `` tag.
#### formValues
-This setting attempts to copy the current values of form elements into the printThis iframe. On by default.
-
-Complex field values, such as those containing quotations or mark-up, may have difficulties. Please report any unexpected behavior.
+This setting copies the current values of form elements into the printThis iframe. On by default.
#### canvas -- Experimental
As of 1.9.0 you may be able to duplicate canvas elements to the printThis iframe. Disabled by default.
@@ -123,8 +121,4 @@ $("#mySelector").printThis({
* Every user should be active in the debugging process
## ToDo:
-* Look at more efficient form field value persist
* Look at alternative to setTimeout ($.deferred?)
-
-
-
diff --git a/printThis.js b/printThis.js
index 1361cc1..1238f16 100644
--- a/printThis.js
+++ b/printThis.js
@@ -49,7 +49,14 @@
function appendBody($body, $element, opt) {
// Clone for safety and convenience
- var $content = $element.clone();
+ // Calls clone(withDataAndEvents = true) to copy form values.
+ var $content = $element.clone(opt.formValues);
+
+ if (opt.formValues) {
+ // Copy original select and textarea values to their cloned counterpart
+ // Makes up for inability to clone select and textarea values with clone(true)
+ copyValues($element, $content, 'select, textarea');
+ }
if (opt.removeScripts) {
$content.find('script').remove();
@@ -57,15 +64,24 @@
if (opt.printContainer) {
// grab $.selector as container
- $body.append($("").html($content).html());
+ $content.appendTo($body);
} else {
// otherwise just print interior elements of container
$content.each(function() {
- $body.append($(this).html());
+ $(this).children().appendTo($body)
});
}
}
+ // Copies values from origin to clone for passed in elementSelector
+ function copyValues(origin, clone, elementSelector) {
+ var $originalElements = origin.find(elementSelector);
+
+ clone.find(elementSelector).each(function(index, item) {
+ $(item).val($originalElements.eq(index).val());
+ });
+ }
+
var opt;
$.fn.printThis = function(options) {
opt = $.extend({}, $.fn.printThis.defaults, options);
@@ -205,55 +221,6 @@
});
}
- // capture form/field values
- if (opt.formValues) {
- // loop through inputs
- var $input = $element.find('input');
- if ($input.length) {
- $input.each(function() {
- var $this = $(this),
- $name = $(this).attr('name'),
- $checker = $this.is(':checkbox') || $this.is(':radio'),
- $iframeInput = $doc.find('input[name="' + $name + '"]'),
- $value = $this.val();
-
- // order matters here
- if (!$checker) {
- $iframeInput.val($value);
- } else if ($this.is(':checked')) {
- if ($this.is(':checkbox')) {
- $iframeInput.attr('checked', 'checked');
- } else if ($this.is(':radio')) {
- $doc.find('input[name="' + $name + '"][value="' + $value + '"]').attr('checked', 'checked');
- }
- }
-
- });
- }
-
- // loop through selects
- var $select = $element.find('select');
- if ($select.length) {
- $select.each(function() {
- var $this = $(this),
- $name = $(this).attr('name'),
- $value = $this.val();
- $doc.find('select[name="' + $name + '"]').val($value);
- });
- }
-
- // loop through textareas
- var $textarea = $element.find('textarea');
- if ($textarea.length) {
- $textarea.each(function() {
- var $this = $(this),
- $name = $(this).attr('name'),
- $value = $this.val();
- $doc.find('textarea[name="' + $name + '"]').val($value);
- });
- }
- } // end capture form/field values
-
// remove inline styles
if (opt.removeInline) {
// $.removeAttr available jQuery 1.7+