Jump to content

Whipe a div empty before inserting new


TeddyKiller

Recommended Posts

When completing a form, it goes through this process below. #changePassMessage gets a message.. eg: 'Your password has successfully been changed' but if I click submit again, it puts the message next to the message already there.

 

What I want is to whipe that div before putting the new content in. How can I do it?

 

    $("#changePass").click(function() {
        $("#changePassLoading").slideDown("slow").html('<span class="loading">loading</span>');

        $.ajax({
            type: "POST",
            url: "modules/forgotPassword/changePassword.php",
            data: {pass: $("#newPass").val(), confirmPass: $("#confirmPass").val(), email: $("#changePassEmail").val()},
            cache: false,
            success: function(html){
                $("#changePassMessage").prepend(html);
                $("#changePassMessage").slideDown("slow");
                document.getElementById('content').value='';
                document.getElementById('content').focus();
                $("#changePassLoading").hide();
            }
        });
        return false;
    });

Link to comment
Share on other sites

                $("#changePassMessage").prepend(html);

This line is adding to the end of it (prepend). I'm not sure what the jQuery function is to just replace, but the standard JavaScript function would look like this:

document.getElementById('changePassMessage').innerHTML = html;

That would replace whatever is in there with your "html" variable.

Link to comment
Share on other sites

One method I frequently use for apps like this is to disable buttons when they are clicked, and have the Ajax success function re-enable it after the code has finished executing. This disallows users from submitting over and over while the script is still running.

Link to comment
Share on other sites

So are you saying that you are trying to append to what ever is in the div, as long as it's not "Loading..."? If so, you're gonna have to something different. A few options:

  • Create logic that removes the last "Loading..." before appending with the requested data (not recommended, because you'll never know if that would remove any wanted data)
  • Create an additional place to put these messages and remove them whenever you want to, like maybe a jQuery dialog.
  • Or, the easiest, do the disable button thing that I suggested before, but in addition, change the button text temporarily to "Loading..." until it's done loading and then swap it back to whatever the button text (value) was before.

Link to comment
Share on other sites

    $("#changePass").click(function() {
        document.getElementById('changePass').disabled = true;
        document.getElementById('changePass').value = 'Loading...';

        $.ajax({
            type: "POST",
            url: "modules/forgotPassword/changePassword.php",
            data: {pass: $("#newPass").val(), confirmPass: $("#confirmPass").val(), email: $("#changePassEmail").val()},
            cache: false,
            success: function(html){
                $("#changePassMessage").prepend(html);
                $("#changePassMessage").slideDown("slow");
                document.getElementById('content').value='';
                document.getElementById('content').focus();
                $("#changePassLoading").hide();
                document.getElementById('changePass').disabled = false;
                document.getElementById('changePass').value = 'Submit';
            }
        });
        return false;
    });

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.