How To Create An Editable Label

Introduction

 
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”>  
  2.     <h2>This is sample code</h2>  
  3. </div>      
  4. document.getElementById(“parentDiv”).innerHTML=”  
  5. <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 a span tag with the content. I am using a link (<a href=”"/> tag to change the span into the input field. With jQuery. Let us see in detail.
 
Steps
  1. Create html view with span: (i have created 3 fields, Name, title, email address ),
     
    view
     
    Code
    1. <div class="content">  
    2.     <h3>Editable Label Example</h3>  
    3.     <div class="fields">  
    4.         <span>Username :</span>  
    5.         <br />  
    6.         <div class="editbox">  
    7.             <span class="editlabel">enter username</span>  
    8.             <!-- <input type="textbox" class="txtbox" placeholder="enter username">-->  
    9.             <a class="lnkbtn" href="#">  
    10.                 <span class="role">Edit</span>  
    11.             </a>  
    12.         </div>  
    13.     </div>  
    14.     <div class="fields">  
    15.         <span>Title :</span>  
    16.         <br />  
    17.         <div class="editbox">  
    18.             <span class="editlabel">enter title</span>  
    19.             <a class="lnkbtn" href="#">  
    20.                 <span class="role">Edit</span>  
    21.             </a>  
    22.         </div>  
    23.     </div>  
    24.     <div class="fields">  
    25.         <span>Email :</span>  
    26.         <br />  
    27.         <div class="editbox">  
    28.             <span class="editlabel">enter email address</span>  
    29.             <a class="lnkbtn" href="#">  
    30.                 <span class="role">Edit</span>  
    31.             </a>  
    32.         </div>  
    33.     </div>  
    34.     <div class="fields">  
    35.         <input type="button" value="DONE">  
    36.         </div>  
    37.         <div>   
  2. Now create jquery function (don't forget to inlude jquery library):
    1. < script type = "text/javascript" > $(document).ready(function() {  
    2.     $(".lnkbtn").click(function() {  
    3.         var role = $(this).find('.role');  
    4.         var value = $(this).siblings();  
    5.         var values = value.text();  
    6.         if(role.html() == "Edit") {  
    7.             role.html("Save");  
    8.             if(values == "enter username" || values == "enter title" || values == "enter email address") {  
    9.                 values = "";  
    10.             }  
    11.             $(this).siblings().html('<input type="textbox" class="txtbox" value="' + values + '" placeholder="enter username">');  
    12.         } else {  
    13.             if(value.find('.txtbox').val() != "") {  
    14.                 $(this).siblings().html('<span class="editlabel">' + value.find('.txtbox').val() + '</span>');  
    15.                 role.html("Edit");  
    16.             } else {  
    17.                 alert("Please Fill the field");  
    18.             }  
    19.         }  
    20.     });  
    21. }); < /script> 
     
    code
     
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 is fetched from the input field.
 
form
 
Note 
 
You can reduce the code, there are a lot of methods to reduce the code. So try to reduce the number of codes.
 
Check out the code that I have attached...
 
If you have any doubts please comment. 
 
Read more articles on JavaScript