Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I catch what I think you're trying to do, it's not quite feasable the way you imagine. However, there are tricks. The following is one of them. The idea is to insert a "hidden" input where the span is, then create functions to show the input and hide span when needed (like when user clicks on span. Something like so:</p> <p><a href="http://jsfiddle.net/SpYk3/Pnr3P/" rel="nofollow"><h1>jsFiddle</h1></a></p> <h2>HTML</h2> <pre><code>&lt;button id="createDiv"&gt;Start&lt;/button&gt; &lt;div id="results"&gt;&lt;/div&gt; </code></pre> <h2>CSS</h2> <pre><code>#createDiv, #results span { cursor: pointer; } #results div { background: #FFA; border: 1px solid; margin: 1em; padding: 1em 1em 2em; } #results input[type=text] { border: none; display: none; outline: none; } </code></pre> <h2>JavaScript</h2> <pre><code>// Call for document .onload event $(function() { // Normal Click event asignement, same as $("#createDiv").click(function $("#createDiv").on("click", function(e) { // Simply creating the elements one by one to remove confusion var newDiv = $("&lt;div /&gt;", { class: "new-folder" }), // Notice, each child variable is appended to parent newInp = $("&lt;input /&gt;", { type: "text", value: "New Folder", class: "title-inp" }).appendTo(newDiv), newSpan = $("&lt;span /&gt;", { id: "myInstance2", text: "New Folder", class: "title-span" }).appendTo(newDiv); // Everything created and seated, let's append this new div to it's parent $("#results").append(newDiv); }); // the following use the ".delegate" side of .on // This means that ALL future created elements with the same classname, // inside the same parent will have this same event function added $("#results").on("click", ".new-folder .title-span", function(e) { // This hides our span as it was clicked on and shows our trick input, // also places focus on input $(this).hide().prev().show().focus(); }); $("#results").on("blur", ".new-folder .title-inp", function(e) { // tells the browser, when user clicks away from input, hide input and show span // also replaces text in span with new text in input $(this).hide().next().text($(this).val()).show(); }); // The following sures we get the same functionality from blur on Enter key being pressed $("#results").on("keyup", ".new-folder .title-inp", function(e) { // Here we grab the key code for the "Enter" key var eKey = e.which || e.keyCode; if (eKey == 13) { // if enter key was pressed then hide input, show span, replace text $(this).hide().next().text($(this).val()).show(); } }); }) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload