Chuck Le Butt
Chuck Le Butt

Reputation: 48826

jQuery $(this).children not working?

I'm sure jQuery works fine, but for some reason it isn't for me. I can't even put it into a jsFiddle because it doesn't like Ajax, but my statements involving $(this).children are not working at all. I'm not getting any errors... what am I doing wrong?

JS

$('.submitNewOrder').submit( function() {
    $(this).children('input[type=\"submit\"]').prop('disabled',true); // Doesn't work
    $(this).children('.saving').fadeIn('fast');  // Doesn't work
    $.ajax({
        url: 'scripts/submitNewOrder.php?catID=1',
        data: newOrder,
        type: 'POST',
        mode: 'abort',
        success: function(){
            $(this).children('.saving').fadeOut('fast', function() { // Doesn't work
                $(this).children('.success').fadeIn('fast'); // Doesn't work
            });
        },
        error: function(){
            $(this).children('.error').fadeIn('fast'); // Doesn't work
        }
    });
    return false;
});

HTML

<form class="submitNewOrder">
    <p>
    <input type="submit" value="Save order" />
        <span class="saving" style="display:none">Saving changes...</span>
        <span class="success" style="display:none">Saved!</span>
        <span class="error" style="display:none">There was a problem saving :(</span>
    </p>
</form> 

Upvotes: 2

Views: 19596

Answers (3)

A. Wolff
A. Wolff

Reputation: 74410

Use context option of ajax and use .find() instead of .children():

$.ajax({
        url: 'scripts/submitNewOrder.php?catID=1',
        data: newOrder,
        type: 'POST',
        mode: 'abort',
        context: this, //<< HERE so in callback functions, 'this' refers to the object, not options object of ajax call
        success: function(){
            $(this).find('.saving').fadeOut('fast', function() {
                $(this).find('.success').fadeIn('fast');
            });
        },
        error: function(){
            $(this).find('.error').fadeIn('fast');
        }
    });

Upvotes: 3

user1864610
user1864610

Reputation:

In submit this points to the form element. The elements you are looking for are not children of form, but children of <p>. Change your expressions to $(this).find(...), which will search further than one level down the tree.

Upvotes: 0

palaѕн
palaѕн

Reputation: 73976

Try replacing the children with find like:

$('.submitNewOrder').submit(function () {
    var $this = $(this);
    $this.find('input[type=\"submit\"]').prop('disabled', true); 
    $this.find('.saving').fadeIn('fast'); 
    $.ajax({
        url: 'scripts/submitNewOrder.php?catID=1',
        data: newOrder,
        type: 'POST',
        mode: 'abort',
        success: function () {
            $this.find('.saving').fadeOut('fast', function () { 
                $this.find('.success').fadeIn('fast'); 
            });
        },
        error: function () {
            $this.find('.error').fadeIn('fast'); 
        }
    });
    return false;
});

Upvotes: 9

Related Questions