javascript - Converting Span to Input -


i developing web app, have such requirement whenever user click on text inside span need convert input field , on blur need convert back span again. using following script in 1 of jsp page.

java script:

<script type="text/javascript"> function covertspan(id){      $('#'+id).click(function() {         var input = $("<input>", { val: $(this).text(),                                    type: "text" });         $(this).replacewith(input);         input.select();        });         $('input').live('blur', function () {             var span=$("<span>", {text:$(this).val()});             $(this).replacewith(span);        });          } 

jsp code:

<span id="loadnumid" onmouseover="javascript:covertspan(this.id);">5566</span> 

now problem is, works fine first time. mean whenever click on text inside span first time converts input field , again onblur coverts input field normal text. if try once again won't work. whats wrong above script?

wanderers - save time, last option @ bottom of answer preferred solution.


i believe looking for: http://jsfiddle.net/2d9fw/

var switchtoinput = function () {     var $input = $("<input>", {         val: $(this).text(),         type: "text"     });     $input.addclass("loadnum");     $(this).replacewith($input);     $input.on("blur", switchtospan);     $input.select(); }; var switchtospan = function () {     var $span = $("<span>", {         text: $(this).val()     });     $span.addclass("loadnum");     $(this).replacewith($span);     $span.on("click", switchtoinput); }; $(".loadnum").on("click", switchtoinput); 

i used class instead of id multiple, can switch if want. if want use id: http://jsfiddle.net/2d9fw/1/


someone upped attention went it. way (changing dom bit): http://jsfiddle.net/2khuobze/

<div class="inputswitch"> first name: <span>john</span><input /> </div> <div class="inputswitch"> last name: <span>doe</span><input /> </div> 

js

$(".inputswitch span").on("click", function() {     var $this = $(this);     $this.hide().siblings("input").val($this.text()).show(); }); $(".inputswitch input").on("blur", function() {     var $this = $(this);     $this.hide().siblings("span").text($this.val()).show(); }).hide(); 

also, if wanted support selecting on focus , tabbing next/previous span/input, (i option best): http://jsfiddle.net/x33gz6z9/

var $inputswitches = $(".inputswitch"),   $inputs = $inputswitches.find("input"),   $spans = $inputswitches.find("span"); $spans.on("click", function() {   var $this = $(this);   $this.hide().siblings("input").show().focus().select(); }).each( function() {   var $this = $(this);   $this.text($this.siblings("input").val()); }); $inputs.on("blur", function() {   var $this = $(this);   $this.hide().siblings("span").text($this.val()).show(); }).on('keydown', function(e) {   if (e.which == 9) {     e.preventdefault();     if (e.shiftkey) {       $(this).blur().parent().prevall($inputswitches).first().find($spans).click();     } else {       $(this).blur().parent().nextall($inputswitches).first().find($spans).click();     }   } }).hide(); 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -