SLAPme
SLAPme

Reputation: 39

JQuery code problem?

New to JQuery, I added the following JQuery code below and moved it around in my code and now it won't work I forgot what I did, can someone fix my code by placing the below code in its correct place thanks.

$('a').click(function () {
    $('#changes-saved').remove();
});
    return false; // prevent normal submit
});

JQuery code.

$(function() {
    $('#changes-saved').hide();
    $('.save-button').click(function() {
        $.post($('#contact-form').attr('action'), $('#contact-form').serialize(), function(html) {
            $('div.contact-info-form').html(html);
            $('#changes-saved').append('Changes saved!').show().pause(1000).hide();
        });
        return false; // prevent normal submit
    });

    $('a').click(function () {
        $('#changes-saved').remove();
    });
        return false; // prevent normal submit
    });
});

Upvotes: -1

Views: 231

Answers (3)

jwwishart
jwwishart

Reputation: 2895

Is this what you wanted? Had an extra )}; at the end and return false; was not inside the click event handler function.

$(function() {
    $('#changes-saved').hide();

    $('.save-button').click(function() {
        $.post(
            $('#contact-form').attr('action'), 
            $('#contact-form').serialize(), 
            function(html) {
                $('div.contact-info-form').html(html);
                $('#changes-saved').append('Changes saved!')
                    .show().pause(1000).hide();
            }
        );

        return false; // prevent normal submit
    });

    $('a').click(function () {
        $('#changes-saved').remove();

        return false; // prevent normal submit
    });
});

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630637

It looks like an extra paste resulting in an extra return and closing block, just remove this at the end:

    return false; // prevent normal submit
});

Upvotes: 1

Buhake Sindi
Buhake Sindi

Reputation: 89209

To prevent <a> from propagating in the browser.

$('a').click(function (event) {
    if (!event) event = window.event;
    if (event.preventDefault)
       event.preventDefault();
    else
       event.returnValue = false;


    $('#changes-saved').remove();

    //In case the event propagation didn't work....(mostly dumb IE)
    return false; // prevent normal submit
});

From your code, just fix

$(function() {
    $('#changes-saved').hide();
    $('.save-button').click(function() {
        $.post($('#contact-form').attr('action'), $('#contact-form').serialize(), function(html) {
            $('div.contact-info-form').html(html);
            $('#changes-saved').append('Changes saved!').show().pause(1000).hide();
        });
        return false; // prevent normal submit
    });
    $('a').click(function () {
        $('#changes-saved').remove();
        return false; // prevent normal submit
    });
    });
});

Upvotes: 0

Related Questions