Frank11
Frank11

Reputation: 65

Jquery attr() is not working second time

jQuery attr() is not working second time, editAsk thing is working but editDone is not working. editDone attribute comes after the process of .editAsk.

    $('.editAsk').on('click', function(){
        var $this = $(this);
        $this.attr('class', 'callEdit editDone');
    });
    $('.editDone').on('click', function(){
        var $this = $(this);    
        $this.attr('class', 'callEdit editAsk');
    });

Upvotes: 0

Views: 730

Answers (2)

pushOk
pushOk

Reputation: 290

$('.callEdit').on('click.doSmth', function(){
    var $that = $(this);
    if ($that.hasClass('editAsk')) {
        // do smth
    } else {
        // do smth else
    }
    $that.toggleClass('editAsk editDone');
});

One event, no need to use delegation. Just another way to do this.

Upvotes: 2

karthikr
karthikr

Reputation: 99630

Use delegation this way:

$(document).on('click', '.editAsk', function(){
    var $this = $(this);
    $this.attr('class', 'callEdit editDone');
});
$(document).on('click', '.editDone', function(){
    var $this = $(this);    
    $this.attr('class', 'callEdit editAsk');
});

Doing it this way, would allow you to delegate the event handler to the existing elements, or any future elements that will be added/modified in the DOM.

You can read more on event delegation using on here

Upvotes: 4

Related Questions