For my project i need a editable text ,so i decided to use some plugins but i am also new to jQuery and decided to create my own editable label [inline edit] .
Here is my code :
When user clicks an element with class editable
$(".editable").live("click",function(){
//alert($(this).text());
//CurrentOBJhtml = $(this).text();
if (typeof CurrentOBJhtml == 'undefined' || CurrentOBJhtml =="" )
{
CurrentOBJhtml = $(this).text();
}
nextHtml = "<input style='border:1px solid red;' type='text' class='hoverable' value='"+CurrentOBJhtml+"' />";
var c = nextHtml;
$(this).html(c);
$(this).children().focus();//$(this).focus();
return false;
});
When user leaves the hoverable
$(".hoverable").live("focusout",function(){
var Hovertext = $.trim($(this).val());
if (Hovertext == null || Hovertext=="")
{
$(this).parent().text(CurrentOBJhtml);
}
else
{
$(this).parent().text(Hovertext);
}
return false;
});
The Problem is when i start edit first element it works well ,but if there is two element with class editable the second one also getting the value of first one ?
Please check the following example :
<label class='editable'>userMania1</label>
<label class='editable'>userDirection1</label>
i can edit the first label ,but when i click the second one i am getting the value of first one so the second one will be <label class='editable'>userMania1</label> which is incorrect.
Please note that i am little bit new to this technology and trying to learn with my current project,how can i solve this problem?
Thank you.
Using global variables makes it difficult to reuse part of your code. In jQuery, you can use
.data()[docs] to associate arbitrary data with a DOM element.Here is a cleaned up version of your code:
DEMO
Also note, since jQuery 1.7, you should use
.on()[docs] instead of.live()and bind the event handlers directly to the elements if they already exist.