user2838698
user2838698

Reputation:

Why the error is coming $this is not defined?

I'm having following code snippet:

$(function(){
    $(document).on('click','.delete_question',function() { 
        var question_id = $this.closest('tr').attr('id');
        var $ele = $this.closest('tr').attr('id');
        $.ajax({
            type:'POST',
            url:'match_question.php',
            data:{'request_type':'ajax', 'op':'delete','question_id':question_id},
            success: function(data) {
                 if(data=="YES") {
                    $ele.fadeOut().remove();
                 }else{
                        alert("can't delete the row");
                 }
             }

            });
        });
});

For the above code I'm getting an error as "ReferenceError: $this is not defined" in a console. But if I print some alert message at the beginning of the function it prints it. Then why the error is coming? Can you help me in this regard please?

Upvotes: 2

Views: 980

Answers (3)

6502
6502

Reputation: 114579

The error message is quite clear, your code is using the variable $this that however was not defined and such an operation is an error. May be there's some confusion about $, this, $this and $(this)...

this

this is a reserved word in Javascript and has a special meaning and special properties. It's used to refer to the "current context" when executing a function and its value is the object when the code executed is from a method call.

For example if you have an object obj the two fragments

obj.foo();

and

m = obj.foo; m();

are not equivalent because when executing the code of foo in the first case this will be obj while in the second case this will be the global object.

$this

$this instead for Javascript is just a variable like any other, there's nothing special about the char $ for Javascript and it can be used freely for identifiers.

$

Like said before the character $ is not special and $ is a valid identifier exactly like a or Q. JQuery happened to choose a single bare $ as a (the) function name because it was short and it wasn't used often (so it was unlikely to collide with other variables).

$(this)

Probably the snipped you took the code from was using $(this) (just guessing)... and it's something completely different from $this because $(this) is the return value of calling the JQuery function $ passing the current value of this.

Upvotes: 4

Mike Barwick
Mike Barwick

Reputation: 5367

Add brackets around this. Less code. :}

Also note: both of those variables produce the exact same string. I'd remove one, making for even less code. ;)

$(function(){
    $(document).on('click','.delete_question',function() { 
        var question_id = $(this).closest('tr').attr('id');
        var $ele = $(this).closest('tr').attr('id');

        ......
    )};
)}; 

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388436

$(function () {
    $(document).on('click', '.delete_question', function () {
        //the variable $this needs to be defined
        var $this = $(this);
        //also $ele need to refer to the tr element not the id
        var $ele = $this.closest('tr');
        var question_id = $ele.attr('id');
        $.ajax({
            type: 'POST',
            url: 'match_question.php',
            data: {
                'request_type': 'ajax',
                    'op': 'delete',
                    'question_id': question_id
            },
            success: function (data) {
                if (data == "YES") {
                    $ele.fadeOut().remove();
                } else {
                    alert("can't delete the row");
                }
            }
        });
    });
});

Upvotes: 2

Related Questions