Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Created February 22, 2014 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james2doyle/9163643 to your computer and use it in GitHub Desktop.
Save james2doyle/9163643 to your computer and use it in GitHub Desktop.
a small jquery plugin that causes elements to turn it into a input fields
$.fn.extend( {
swappable: function(options) {
this.defaults = {
id: 'swappable-temp-input',
class: 'swappable-input',
name: 'swappable-temp',
type: 'input',
evt: 'click',
end: 'blur',
detectTag: true,
swapStart: function() {},
swapEnd: function() {}
};
var settings = $.extend({}, this.defaults, options);
function makeTemplate() {
if (settings.type === 'input') {
return '<input type="text" class="'+settings.class+'" name="'+settings.name+'" id="'+settings.id+'" value="'+settings.temp+'">';
} else if(settings.type === 'textarea') {
return '<textarea class="'+settings.class+'" name="'+settings.name+'" id="'+settings.id+'">'+settings.temp+'</textarea>';
} else {
// something is messed. let there be inputs
return '<input type="text" class="'+settings.class+'" name="'+settings.name+'" id="'+settings.id+'" value="'+settings.temp+'">';
}
}
function handleClick() {
var $this = $(this);
settings.swapStart();
settings.temp = $this.text();
settings.tag = (typeof(settings.detectTag) !== 'string') ? $this.prop('tagName'): settings.detectTag;
$this.replaceWith(makeTemplate());
var $input = $('#'+settings.id);
// when you focus the new input/textarea
$input.on(settings.end, function() {
var $p = $('<'+settings.tag+'>'+settings.temp+'</'+settings.tag+'>');
$(this).replaceWith($p);
// call itself with the settings from the first run
$p.swappable(settings);
settings.swapEnd();
}).focus();
}
return this.each(function() {
$(this).on(settings.evt, handleClick);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment