Kamil Sindi
Kamil Sindi

Reputation: 22862

Can't reference "this" when using onreset in Jeditable

For some reason I can't refer to the element I'm editing as $(this) when I use the onreset handler.

However, I can use use $(this) in my callback. I'm sure onreset works because I've done an alert. Furthermore, when I do an alert on $(this).attr('id') I get "undefined".

What's going on?

CODE

    $('.edit').editable('ajax/save.php?editnotetext', {
                type : 'mce',
                submit : '<button class="save_button">Save</button>',
                cancel : '<button class="cancel_button">Cancel</button>',
                event: 'dblclick',
                placeholder : 'Doubleclick to edit...',
                indicator : 'Saving...',
                tooltip : 'Doubleclick to edit...',
                onblur: 'custom',
                callback : function(){
                          console.log('unlocked');
                          $.post('ajax/save.php?unlocknotetext', {"id" : $(this).attr('id')});
                },
                onreset : function(){
                          console.log('unlocked');
                          //myId = $(this).attr('id');
                          //alert(myId); this shows up as undefined!
                          //alert("onreset works!");
                          $.post('ajax/save.php?unlocknotetext', {"id" : $(this).attr('id')});
                }

});

Upvotes: 0

Views: 592

Answers (2)

bpelhos
bpelhos

Reputation: 2148

This worked for me in the onReset section:

    curr_form = this[0];         //form object
    par = curr_form.parentNode;  //parent,container object
    alert(par.id);

Upvotes: 0

Eric
Eric

Reputation: 97691

this comment seems to explain it:

It seems inside onreset and onsubmit, this points to the form, not its container, so you have to use $(this).parent() instead.

An easy fix would be to do this:

$('.edit').each(function() {
    var $this = $(this);
    $this.editable(... /* use $this instead of $(this) here*/)
});

Upvotes: 2

Related Questions