views:

3419

answers:

3

Hi everyone!

I'm trying to create a text edit in place myself just by manipulating jQuery selectors. But, after the first ajax callback, the others callback for the same input text doesn't work anymore...

function editaVal(celDiv, id)
    {
        var novoConteudo = $("<input type='text' id='novoCont" + id + "' value='" + $(celDiv).html() + "' />");
        $(celDiv).dblclick(function()
        {
            $(this).html(novoConteudo);
        });

        $(novoConteudo).blur(function()
        {
            $(novoConteudo).parents("tr").removeClass('trSelected');
            $.ajax({
                type: 'POST',
                url: '/SuperAdmin/SalvaValor/?busca=' + $(novoConteudo).val() + '&codValor=' + id,
                beforeSend: function()
                {
                    $(celDiv).html($("#loading").show());
                },
                success: function()
                {
                    $(celDiv).html($("#loading").hide());
                    $(celDiv).html(novoConteudo.val());
                }
            });
        });
    }

My question is: how can i rebind this??? Rebind the blur event... When I blur the input text, nothing happens on the second ajax callback.

thanks!!

+3  A: 

You could use jQuery.live, (jQuery 1.3+). It binds a handler to an event for all current and future matched elements.

CMS
I've tried to use livequery plugin but nothing happened. I'm using jQuery 1.2.6 and unfortunaly I can't upgrade it right now!
AndreMiranda
+2  A: 

If you create a single function that sets up the page or rebinds the controls and then call that function on Success of the Ajax call, that may work.

EDIT
Give this a try...

success: function()                
{                    
     $(celDiv).html($("#loading").hide());                    
     $(celDiv).html(novoConteudo.val());     
     ConfigMyInputs();           
}

function ConfigMyInputs()
{
     //foreach item
     //configure double click
     //configure blur
}

function MakeMyAjaxCall() {
    //AJAX CODE HERE
}
RSolberg
I've tried to do that on my example, but with no success... don't know if I did it right!!
AndreMiranda
+2  A: 

You're attaching a "onblur" handler to an element that gets wiped out after the ajax update. If you want the event to persist, just bind it when you create it (ie on the dblclick):

function editaVal(celDiv, id)
{
    $(celDiv).dblclick(function()
    {
        var text = $(this).html();
        var novoConteudo = $('<input type="text" />')
            .appendTo($(this).empty())
            .attr('id', 'novoCont' + id)
            .val(text)
            .blur(function()
            {
                $(this).parents("tr").removeClass('trSelected');
                $.ajax({
                    type: 'POST',
                    url: '/SuperAdmin/SalvaValor/?busca=' + $(this).val() + '&codValor=' + id,
                    beforeSend: function()
                    {
                        $(celDiv).html($("#loading").show());
                    },
                    success: function()
                    {
                        $(celDiv).html($("#loading").hide());
                        $(celDiv).html(novoConteudo.val());
                    }
                });
            });
    });
}
Crescent Fresh
oh!! Thanksssssssss!! It was very simple but I couldn't see that!! :-)
AndreMiranda