How To Create An Editable Label

In this article you will learn how to create an editable label.

Let us see how to create an editable label in JavaScript and CSS. Here, I am using a method to change the label into input field using innerHTML, This method is used to change the inner elements of a node. Suppose there is an h2 tag inside a tag, we can change the h3 into another element using the following code :

  1. <div id=”parentDiv”><h2>This is sample code</h2></div>  
  2. document.getElementById(“parentDiv”).innerHTML=”<h5>Some Text</h5>”;  
So the <h2> tag will be changed into <h5> tag with new text 'Some Text'.

Here we are using the same method. First our text will be in span tag with the content. I am using a link (<a href=”” /> tag to change the span into input field. With jQuery. Let us see in detail.

  1. Create html view with span: (i have created 3 fields, Name, title, email address ),


    1. <div class="content">  
    2. <h3>Editable Label Example</h3>  
    3. <div class="fields">  
    4. <span>Username :</span><br />  
    5. <div class="editbox">  
    6. <span class="editlabel">enter username</span>  
    7. <!-- <input type="textbox" class="txtbox" placeholder="enter username">-->  
    8. <a class="lnkbtn" href="#"><span class="role">Edit</span></a>  
    9. </div>  
    10. </div>  
    12. <div class="fields">  
    13. <span>Title :</span><br />  
    14. <div class="editbox">  
    15. <span class="editlabel">enter title</span>  
    16. <a class="lnkbtn" href="#"><span class="role">Edit</span></a>  
    17. </div>  
    18. </div>  
    20. <div class="fields">  
    21. <span>Email :</span><br />  
    22. <div class="editbox">  
    23. <span class="editlabel">enter email address</span>  
    24. <a class="lnkbtn" href="#"><span class="role">Edit</span></a>  
    25. </div>  
    26. </div>  
    28. <div class="fields">  
    29. <input type="button" value="DONE">  
    30. </div>  
    31. <div>  
  2. Now create jquery function (don't forget to inlude jquery library):
    1. <script type="text/javascript">  
    2.     $(document).ready(function()   
    3.     {  
    4.         $(".lnkbtn").click(function()  
    5.         {  
    6.             var role = $(this).find('.role');  
    7.             var value = $(this).siblings();  
    8.             var values = value.text();  
    9.             if (role.html() == "Edit") {  
    10.                 role.html("Save");  
    11.                 if (values == "enter username" || values == "enter title" || values == "enter email address") {  
    12.                     values = "";  
    13.                 }  
    14.                 $(this).siblings().html('<input type="textbox" class="txtbox" value="' + values + '" placeholder="enter username">');  
    15.             } else {  
    16.                 if (value.find('.txtbox').val() != "") {  
    17.                     $(this).siblings().html('<span class="editlabel">' + value.find('.txtbox').val() + '</span>');  
    18.                     role.html("Edit");  
    19.                 } else {  
    20.                     alert("Please Fill the field");  
    21.                 }  
    22.             }  
    23.         });  
    24.     });  
    25. </script>


Code Description :

When we click the Edit Button(link), it will store the value of its siblings (span/input field) into a variable, if the role of the link button is EDIT, then link button name will be changed to SAVE, and int siblings inner html is changed to input text with the value that we have stored before. After editing the text, click on the save button (same link button), now the role of the button is saved, the else part will be executed.The textbox is changed to span with the value that are fetched from the input field.



You can reduce the code, there are lot of methods to reduce the code. So try to reduce the number of codes.

Check out my code that I have attached...

If you have any doubts please comment. 

Read more articles on JavaScript