Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Span to Input

I am developing web app, I have such a requirement that whenever user click on text inside span i need convert it into input field and on blur i need to convert it back to span again. So i am using following script in one of my 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 my problem is, everything works fine only for the first time. I mean whenever i click on the text inside span for the first time it converts into input field and again onblur it coverts back from input field to normal text. But if try once again to do so it won't work. Whats wrong with above script?

like image 508
Kishan_KP Avatar asked May 04 '13 12:05

Kishan_KP


People also ask

How do I change input to span on click?

First, you need to change your click handler to use live() as well. You should take note, though, that live() has been deprecated for quite a while now. You should be using on() in both cases instead. Secondly, when you replace the input with the span, you don't give the element an id.

How do you put span inside input tag?

Wrap both your input and your span inside a container, position this container as relative, and the span as absolute. You can now do whatever you like with the span.

Why do we use the span?

The <span> HTML element is a generic inline container for phrasing content, which does not inherently represent anything. It can be used to group elements for styling purposes (using the class or id attributes), or because they share attribute values, such as lang .

What is a span in coding?

The <span> tag is an inline container used to mark up a part of a text, or a part of a document. The <span> tag is easily styled by CSS or manipulated with JavaScript using the class or id attribute. The <span> tag is much like the <div> element, but <div> is a block-level element and <span> is an inline element.

What is the difference between * spanswitch and * spanreset?

* spanSwitch = Here the span is replaced by the input, using the span text as the input value; * spanReset = Here the input is replaced by the span, using the input value as the span text; ready as simple as that. See the complete project working below.

What is span?

What is Span ? Span is the algebraic difference between URV and LRV. Span = URV – LRV.

How do I select all spans that start with a spanval?

Use an attribute selector(span). This'll allow you to select all spans who's ID starts with spanval(if not all of them are spans, you can use the attribute selector on its own () which is a tad slower but works just as well).

How do I update a span using jQuery?

You update an input by setting the value property ("val ()" if using jQuery), but a span by setting the innerHTML property ("html ()" if using jQuery), so that code will need to be amended. I added more code. Thank you for your answer You're using .text so that should be ok for updating a span.


1 Answers

Would be good to change your dom structure to something like this (note that the span and the input are side by side and within a shared parent .inputSwitch

<div class="inputSwitch">
First Name: <span>John</span><input />
</div>
<div class="inputSwitch">
Last Name: <span>Doe</span><input />
</div>

Then we can do our JS like this, it will support selecting all on focus and tabbing to get to the next/previous span/input: 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();
like image 94
Dallas Avatar answered Oct 05 '22 23:10

Dallas