user2691986
user2691986

Reputation:

jQuery IF statement not working?

I've been trying for so long but I just can't get the if statement to work. I'm assuming it's a syntax error but I haven't been able to find it when comparing to all of the tutorials and other SO questions.

The end result will be: If the form action is "add_task.php" I want the jQuery to get the contents of another file and prepend it to a specified div.

The alert message is there at the moment as a troubleshooting measure.

Everything that falls under 'else' works fine, it's just the 'if' part that won't run.

    $("form").submit(function(event){
        event.preventDefault();
        var action = $(this).attr("action");
        var dataString = $(this).serialize();
        $.post(action, dataString, function(action){
            if(action == "add_task.php"){
                alert("Add!");
            }else{
                $(event.target).closest(".task_container").load("display_edit.php");
            }
        });                 
    });

Upvotes: 0

Views: 316

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388436

You have a closure variable and the callback parameter named as action, I think what you need is to check whether the url to which the request was submitted was add_task.php in that case rename the callback param to something else

$("form").submit(function(event){
    event.preventDefault();
    var action = $(this).attr("action");
    var dataString = $(this).serialize();
    $.post(action, dataString, function(data){
        if(action == "add_task.php"){
            alert("Add!");
        }else{
            $(event.target).closest(".task_container").load("display_edit.php");
        }
    });                 
});

Upvotes: 1

Related Questions