UID
UID

Reputation: 4514

Jquery ":empty" not working - To check if all <DIV> contains some text or empty

I generated few boxes at runtime, where I want to confirm if all the boxes is "Empty", then the user should not be able to proceed. But if even a single Box contains the correct value (instead of Empty), then user should be able to proceed.

I created the below code:

$(document).ready (function () {
    setProceedState();

});

function setProceedState() {
    if ($('.type').length == $('.type:empty').length) {
        alert("Empty" + $(".type").html());
        $("#stepAutomapConfirm").attr("disabled", true);
        $("#stepAutomapConfirm").addClass("disabled").removeClass("active");
    } else {
        alert("NOT Empty" + $(".type").html());
        $("#stepAutomapConfirm").attr("disabled", false);
        $("#stepAutomapConfirm").addClass("active").removeClass("disabled");
    }
}

Somehow this code is working fine in the fiddle: http://jsfiddle.net/aasthatuteja/xJtAV/

But on my MVC Partial View page its not fully working:

It always give me the correct alert if the boxes are "empty",

BUT when it generates boxes with data in it at runtime, it still shows "EMPTY" alert. I checked the "view source" and "inspect element" too, in source the data do exist, and then, when I manually refresh my page after that it gives the correct alerts "NOT Empty".

Please suggest what I am missing or what can be the alternative for JQuery ":empty" to make the code work in MVC Partial View!

Let me know if you need any other information!

Upvotes: 0

Views: 1090

Answers (1)

Kevin B
Kevin B

Reputation: 95017

You need to re-execute your function after you generate the boxes, on ready only works for boxes that exist at dom ready.

Upvotes: 2

Related Questions