javascript - Turn a div tag into a input box -


i want able ckick on h2 in .editable div , should change input box. can edit , updat input when click out of or press enter. have it, i'm stuck:

jquery:

$('.editable').on('click', function() {   var h3 = $(this)   var input = $('<input>').val(h3.text())    h3.after(input)   h3.hide()    input.on('blur', function(){    })   //blur   //keyup code 13 -- code 27 reset  }) 

jade: (petty html)

  div.edit-menu-page     div.title       h2 edit menu     div.menu-wrap       div.menu-category         div.menu-title           div.editable             h3 meat         div.menu-items           div.menu-row             span.item-description new york striploin             div.control-items               span.item-price 10$               span.delete x 

.

basically want place form element next original element, when you're done, replace form element original element.

something should work:

$(document).on('click', '.editable', function() {     var $wrapper = $('<div class="editing"></div>'),         $form    = $('<form action="#" class="edit-form"></form>'),         $input   = $('<input type="text">');      // place original element inside wrapper:     $(this).after($wrapper);     $(this).remove().appendto($wrapper).hide();      // build form:     $wrapper.append($form);     $form.append($input);     $input.val($(this).text()).focus(); });  $(document).on('submit', '.edit-form', function(e) {     // don't submit form:     e.preventdefault();      var val       = $(this).find('input').val(),         $wrapper  = $(this).parent(),         $original = $wrapper.children().first();      // use text() instead of html() prevent unwanted effects.     $original.text(val);     $original.remove();     $wrapper.after($original);     $original.show();     $wrapper.remove(); }); 

edit: here's jsfiddle: http://jsfiddle.net/c0fb11qw/1/


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Website Login Issue developed in magento -

Can the constants be defined inside a model file of a framework in PHP? -