0

I have a page with two links that open two different modals, the "forgotten password" link opens the "forgotten password" modal and the "tell-a-friend" link opens the "tell-a-friend" modal.

Both modals contain forms that can be submitted.

The problem is if I open the first modal and submit it or close it, I cannot submit the second modal. I can open the second modal, but I cannot submit it.

Please advise what the problem could be!

Here below is the javascript code that resides in separate javascript file, which is then imported into the HTML file. It is not inline javascript, if that would matter.

[code]

var forgottenPasswordDiv;
var tellAFriendDiv;

function clearErrorMessages() {
    $('#errorMessage').text("");        
}

function openForgottenPassword() {
    forgottenPasswordDiv = $('#forgotten-password');
    $('#forgotten-password').load("/Templates/include/new/ajax/modal/forgottenPassword.jsp")
    .dialog(
        {
            autoOpen:false,
            modal:true, 
            position:'left+35% top+20%', 
            width:'330', 
            height:'auto'
        }
    );
    $('#forgotten-password').dialog('open');
}

function closeForgottenPassword() {
    forgottenPasswordDiv.dialog("close");
}

function submitForgottenPassword() {
    clearErrorMessages();
    var email = $('#email').val();
    if (email == null || email == '') {
        $('#errorMessage').text("Please enter your user name or email");
    } else {
        clearErrorMessages();
        /* Ajax Post */
        var formData = $("#forgottenPasswordForm").serialize();
        $.ajax({
            type: "GET",
            url: "/Templates/include/new/ajax/forgottenPassword.jsp", 
            data: formData,
            success: function(data) {
                if (data.error != null) {
                    $("#errorMessage").text(data.error);
                } else {                            
                    $('#forgottenPasswordForm , .info').fadeOut(1000);
                    $("#successMessage").text(data.success);        
                    $("div").removeClass('display-none');
                }
            },
            dataType: 'json'
        });
    }               
}

function openTellAFriend(gunId) {
    tellAFriendDiv = $('#tell-a-friend');
    $('#tell-a-friend').load("/Templates/include/new/ajax/modal/tellAFriend.jsp?id=" + gunId)
    .dialog(
        {
            autoOpen:false,         
            modal:true, 
            position:'center top+10%', 
            width:'330', 
            height:'auto'
        }
    );
    $('#tell-a-friend').dialog('open');
}


function closeTellAFriend() {
    tellAFriendDiv.dialog("close");
}

function submitTellAFriend() {
    clearErrorMessages();
    var yourname = $('#yourname').val();
    var errorMessage = "";
    if (yourname == null || yourname == '') {
        errorMessage += "Please enter your name<br />";                 
    } 

    if (errorMessage != '') { 
        $('#errorMessage').html(errorMessage);
    } else {
        clearErrorMessages();
        /* Ajax Post */
        var formData = $("#tellAFriendForm").serialize();
        $.ajax({
            type: "GET",
            url: "/Templates/include/new/ajax/tellAFriend.jsp", 
            data: formData,
            success: function(data) {
                if (data.error != null) {
                    $("#errorMessage").text(data.error);
                } else {
                    $("#tellAFriendForm").fadeOut(1000);                        
                    $("#successMessage").text(data.success);
                    $("div").removeClass('display-none');
                }
            },
            dataType: 'json'
        });
    }
}

[/code]

1 Answer 1

1

The ui-dialog widget will stay in the DOM as a hidden element even after the dialog is closed. So, in order to isolate your two dialog functionalities from each other I'd suggest that you call:

forgottenPasswordDiv.dialog("destroy")

in your "closeForgottenPassword" function and

tellAFriendDiv.dialog("destroy")

in your "closeTellAFriend" function.

This will return the dialog back to its pre-init state (which is not harmful at all because you reinit it in your "open" functions.)

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Marty, code function closeForgottenPassword() { forgottenPasswordDiv.dialog("destroy").remove(); var div = '<div id="forgotten-password"></div>'; $("#footer").append(div); } code When remove() is called the <div id="forgotten-password"> will be removed from the DOM, then I create a new one and append it to the footer div. The #forgotten-password and the #tell-a-friend divs are in the footer div in the beginning, so they must be put back so that a dialog can be opened. However, I am not happy calling remove() and adding the div back to the footer, there must be a cleaner solution.
Hi Marty, I forgot to mention that did try your suggested solution, but it still did not work, so I changed both of the close functions. Here is an example code function closeForgottenPassword() { forgottenPasswordDiv.dialog("destroy").remove(); var div = '<div id="forgotten-password"></div>'; $("#footer").append(div); } code . Pleas read my previous message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.