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.
CurrentOBJhtmlis global. You should create a variable local to your plugin function (if you have created a proper jQuery plugin, which might not be the case given the code you've posted). Alternatively you can use.data(): api.jquery.com/data.data()to store the value per element instead of a global variable.