How to make <li> editable by click?

I have a menu that looks like this:

enter image description here

I want that when you click the button, the +element <li>in which it was inside becomes like an editable input form where you can create and save a new name for the menu item (by entering the name and press "Enter").

Current item code

<li id="addNewContext" class="menu-item-divided"><a href="javascript:">+</a></li>

Is there a piece of code that I could use to edit this field that could save the name entered into the array, which can then be used to refill the menu?

Thank!

+4
source share
2 answers

HTML5 ContentEditable HTML. onclick ContentEditable true.

https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_Editable

var list = document.querySelector('ul');
var editList = document.querySelector('.edit-list');

editList.onclick = function() {
  
  //or you can use list.setAttribute("contentEditable", true);
  list.contentEditable = true;

}
<ul>
  <li>list item</li>
  <li>List item</li>
  <li>List item</li>
  <li>List item</li>
  <li>List item</li>
  <li><span class="edit-list">+</span>
  </li>

</ul>
Hide result

JSFiddle: http://jsfiddle.net/b8m35wwk/1/

+4

, , <li>, , , ( Enter).

+ :

$('#addNewContext')

// on click, make content editable.
.click(function() {
    $(this).html("").attr('contenteditable', 'true');
})

// on hit enter, 
.keyup(function(e) {
  if (e.keyCode == 13) {
     var val = $(this).text();
     $(this)
        // create a new li item
        .before("<li>" + val + "</li>")
        // set plus sign again
        .html("+")
        // make contenteditable to false, when clicked the process start again.
        .attr('contenteditable', 'false');
     e.preventDefault();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>not edit me</li>
  <li>not edit me</li>
  <li>not edit me</li>
  <li>not edit me</li>
  <li id="addNewContext" class="menu-item-divided">+</li>
</ul>
Hide result
+1

All Articles