Skip to content
Permalink
Browse files

Replce clone method with one that retains select and textarea values …

…when cloning - reslove #99 - bump to v1.6.0
  • Loading branch information...
sathvikp committed Aug 21, 2018
1 parent 4a72d40 commit ca71856b1bf566d256af7ee0541593e55bf13943
Showing with 32 additions and 9 deletions.
  1. +1 −1 bower.json
  2. +2 −2 demo/index.html
  3. +28 −5 jQuery.print.js
  4. +1 −1 package.json
@@ -1,7 +1,7 @@
{
"name": "jQuery.print",
"main": "dist/jQuery.print.min.js",
"version": "1.5.1",
"version": "1.6.0",
"homepage": "https://doersguild.github.io/jQuery.print/",
"authors": [
"Sathvik P <sathvik@doersguild.com>"
@@ -79,9 +79,9 @@ <h3 class='avoid-this'>Element 4</h3>
//Don't print this
noPrintSelector : ".avoid-this",
//Add this at top
prepend : "Hello World!!!<br/>",
prepend : "Hello World!!!&lt;br/&gt;",
//Add this on bottom
append : "<br/>Buh Bye!",
append : "&lt;br/&gt;Buh Bye!",
//Log to console when printing is done via a deffered callback
deferred: $.Deferred().done(function() { console.log('Printing done', arguments); })
});
@@ -1,17 +1,40 @@
/* @license
* jQuery.print, version 1.5.1
* jQuery.print, version 1.6.0
* (c) Sathvik Ponangi, Doers' Guild
* Licence: CC-By (http://creativecommons.org/licenses/by/3.0/)
*--------------------------------------------------------------------------*/
(function ($) {
"use strict";
// A nice closure for our definitions

function jQueryCloneWithSelectAndTextAreaValues(elmToClone, withDataAndEvents, deepWithDataAndEvents) {
// Replacement jQuery clone that also clones the values in selects and textareas as jQuery doesn't for performance reasons - https://stackoverflow.com/questions/742810/clone-isnt-cloning-select-values
// Based on https://github.com/spencertipping/jquery.fix.clone
var $elmToClone = $(elmToClone),
$result = $elmToClone.clone(withDataAndEvents, deepWithDataAndEvents),
$my_textareas = $elmToClone.find('textarea').add($elmToClone.filter('textarea')),
$result_textareas = $result.find('textarea').add($result.filter('textarea')),
$my_selects = $elmToClone.find('select').add($elmToClone.filter('select')),
$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) {
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;
}

function getjQueryObject(string) {
// Make string a vaild jQuery thing
var jqObj = $("");
try {
jqObj = $(string)
.clone();
jqObj = jQueryCloneWithSelectAndTextAreaValues(string);
} catch (e) {
jqObj = $("<span />")
.html(string);
@@ -181,15 +204,15 @@
$styles = $.merge($styles, $('<link rel="stylesheet" href="' + options.stylesheet + '">'));
}
// Create a copy of the element to print
var copy = $this.clone();
var copy = jQueryCloneWithSelectAndTextAreaValues($this);
// Wrap it in a span to get the HTML markup string
copy = $("<span/>")
.append(copy);
// Remove unwanted elements
copy.find(options.noPrintSelector)
.remove();
// Add in the styles
copy.append($styles.clone());
copy.append(jQueryCloneWithSelectAndTextAreaValues($styles));
// Update title
if (options.title) {
var title = $("title", copy);
@@ -2,7 +2,7 @@
"name": "jQuery.print",
"filename": "jQuery.print.min.js",
"main": "jQuery.print.js",
"version": "1.5.1",
"version": "1.6.0",
"homepage": "https://doersguild.github.io/jQuery.print/",
"authors": [
"Sathvik P <sathvik@doersguild.com>"

0 comments on commit ca71856

Please sign in to comment.
You can’t perform that action at this time.