Red
Red

Reputation: 6378

javascript global variable UNSET

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.

Upvotes: 0

Views: 942

Answers (4)

Red
Red

Reputation: 6378

I just solve it :)

I know this is not the exact way to achieve this & it is dirty.

But for me it works well

I added the following to the click function

if ($(this).children().html() == null)
 {
    //alert('I am editable');
    CurrentOBJhtml = $(this).text();
 }

NOTE : Please use Felix's solution ,it is very neat and better one.

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816334

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:

(function() {
    var $input = $('<input style="border:1px solid red;" type="text" />');
    $input.focusout(function(event) {
        event.stopPropagation();
        event.preventDefault();

        var value = $.trim($(this).val()) || $(this).parent().data('orig_text');
        $(this).parent().text(value);
    });


    $(".editable").live("click",function(event){
        if (event.target === this) {
            event.stopPropagation();
            event.preventDefault();

            var text = $(this).text();

            $(this)
             .data('orig_text', text)
             .empty()
             .append($input.clone(true).val(text))
             .children('input').focus();
        }
    });

}());

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.

Upvotes: 1

alrusdi
alrusdi

Reputation: 1283

avoid to use global variables but if you desided to use them - use carefully

$(".hoverable").live("blur",function(){
    var Hovertext = $.trim($(this).val());
    if (Hovertext == null || Hovertext=="")
    {
        $(this).parent().text(CurrentOBJhtml);
    }
    else
    {
        $(this).parent().text(Hovertext);
    }
    CurrentOBJhtml = "" //<------ NOTE THIS
    return false;
});

Upvotes: 0

Yazan Malkawi
Yazan Malkawi

Reputation: 501

thats due to the following line

nextHtml = "<input style='border:1px solid red;' type='text' class='hoverable' value='"+CurrentOBJhtml+"'  />";

You are assigning the value to CurrentOBJhtml which is set while editing the first one the correct way to do it is:

nextHtml = "<input style='border:1px solid red;' type='text' class='hoverable' value='"+$(this).text();+"'  />";

Or you can set CurrentOBJhtml to null at the end of the onfocusout function

Upvotes: 1

Related Questions