Created
January 24, 2015 18:58
-
-
Save volter9/fb44cc576a5cbb75e1a1 to your computer and use it in GitHub Desktop.
First version of my Form-Preview plugin for jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function () { | |
var view = $('#preview'), | |
elements = $('.preview-form form').find('select, input, textarea'); | |
function preview (specific) { | |
if (view.hasClass('off')) { | |
return; | |
} | |
if ( !specific ) { | |
specific = elements; | |
} | |
specific.each(function () { | |
var self = $(this); | |
var value = self.val(); | |
var element = view.find('[data-preview=' + self.attr('name') + ']'); | |
var attribute = element.attr('data-attr'), | |
pattern = element.attr('data-pattern'); | |
if (pattern) { | |
value = pattern.replace('%s', value); | |
} | |
if (!attribute) { | |
element.html(value); | |
} | |
else { | |
element.attr(attribute, value); | |
} | |
}); | |
} | |
view.find('a').attr('target', '_blank'); | |
elements.on('input', function () { | |
var self = $(this); | |
preview(self); | |
}); | |
preview(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment