vaibhav shah vaibhav shah - 20 days ago 7
Javascript Question

How to check if textbox is empty in jquery dialog

I have one jquery dialog in my app.

Here Is how It is made

var routePopup = "Route Name : <input type='text' id='routeName' name='routeName'/>";


document.getElementById('popUp').innerHTML = "";
document.getElementById('popUp').innerHTML = routePopup;

$("#popUp").dialog({
title: "Route Name",
resizable: false,
width: auto,
height: auto,
buttons: {
"Create Route": function () {
$(this).dialog("close");
}
}
});


now I want to check if textbox is empty or not. If it is empty then it should not allow to close the popup.

Also It should give message on top that the value can not be empty.

Answer

Why are you writing document.getElementById('popUp').innerHTML = ""; if you could do $('#popUp').html(''); - that's why you use jQuery :)

$("#popUp").dialog({
        title: "Route Name",
        resizable: false,
        width: auto,
        height: auto,
        buttons: {
            "Create Route": function () {
                if ($(this).val() == '') {
                    alert('May not be empty!');
                }
                else {
                    $(this).dialog("close");
                }
            }
        }
    });