Skip to content

Commit

Permalink
Merge pull request #120 from Subtletree/user-clone(true)-to-copy-form…
Browse files Browse the repository at this point in the history
…-values

Use clone(true) to copy form values
  • Loading branch information
oculus42 authored Oct 30, 2017
2 parents 349dc9d + c47e45f commit eebda06
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 61 deletions.
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ A string or jQuery object to prepend or append to the printThis iframe content.
$('#mySelector').printThis({
header: "<h1>Amazing header</h1>"
});

$('#mySelector').printThis({
footer: $('.hidden-print-header-content')
});
Expand All @@ -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 `<base>` attribute will be set if one exists on the page.
If set to `true`, a `<base>` 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 `<base>` 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.
Expand Down Expand Up @@ -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?)



71 changes: 19 additions & 52 deletions printThis.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,39 @@

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();
}

if (opt.printContainer) {
// grab $.selector as container
$body.append($("<div/>").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);
Expand Down Expand Up @@ -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+
Expand Down

0 comments on commit eebda06

Please sign in to comment.