Question

I cannot get a modal to hide after confirming a delete (closing works fine), thus I am trying to reference it by a modal ID now.

From what I am reading, the typical way to hide a modal is $('#myModal').modal('hide'); However this does not seem to be working for me, I used $('.modal').modal('hide'); and I am wondering if it is because I need to specify the ID # of the modal, but I am not sure what the "correct" way of doing that is if my ID is an @model attribute`

Here is my button

<button class="btn btn-danger col-lg-3 col-lg-offset-3" data-toggle="modal" data-target="#@item.ID" onclick="deleteStart(this)">

Here is my initial div class for my modal

<div class="modal fade" id="@item.ID" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

Here is my JQuery that is the onClick for my Confirm Delete option(I have a few attempts within the code)

function deleteFunction(element) {
var newID = $(element).closest("td").find("span.ID").text();

$.post(
'@Url.Action("customDelete", "Show")',
{
    'id': newID
},
function (data) { },
"json"
);
        $(element).closest("tr").next("tr").remove();
        $(element).closest("tr").remove();

        //$(element).attr("data-target", "#" + newID).modal('hide');
        //$('#newID').modal('hide');
        //$('.modal').modal('hide');

        $(element).closest("table").toggleClass("table-hover");
        $(element).closest("tr").css('background-color', 'initial');
    }

NOTE: the var newID applies to the current item's object ID (item.ID), but since that is what my modal ID is set to as well, shouldn't it be working?

item.ID is my model's primary key so it is unique too.

Was it helpful?

Solution

Fixed! The culprit was modal's fade.

In order to get this to work I had to use the following within a function that was called by the Confirm button. This has to be done BEFORE anything else, especially AJAX. Apparently AJAX can mess up some of the scripting for Modal's so you gotta finish all your modal stuff before you do other things.

Function Snippet

$('#myModal').removeClass('fade');
$('#myModal').modal('hide');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top