JSFiddle - React, Tailwind, and code Playground
HTML
<p class="editable"></p>
JavaScript
/**
* inplaceTextEdit - edit text in place.
*
*/
(function($) {
$.fn.inplaceTextEdit = function(options) {
options = $.extend({
showPlaceholder: true,
showHints: true,
hintText: 'Hit enter to save',
hintId: 'editable_text_hint',
placeholderText: 'Click here to edit...',
textInputType: 'input',
textInputId: 'editable_text_input',
onEditBegin: null,
onSave: null,
onExitWithoutSave: null
}, options);
$(this).each(function() {
var that = $(this)
, currentText = ''
, validInputTypes = ['input', 'textarea']
, inEditMode = false;
// Validate the given input type.
options.textInputType = ($.inArray(options.textInputType, validInputTypes)) ? options.textInputType : 'textarea';
// If there is no current editable value then show the placeholder if allowed.
if(that.html() === '' && options.showPlaceholder) { that.html(options.placeholderText); }
/**
* Function to handle the saving of the editable text.
*
* @method saveEditableText
* @private
* @void
*
*/
var saveEditableText = function() {
var input = that.find(options.textInputType);
if(input.val() !== currentText) {
currentText = input.val();
input.remove();
if(options.showHints) { $('#' + options.hintId).remove(); }
that.html(((currentText !== '') ? currentText : options.placeholderText));
inEditMode = false;
executeCallback(options.onSave,...