Critical Developers

Programmers Knowledge Base

Check if any input element has a value in a div/form using jQuery

Check if any input element has a value in a div/form using jQuery

function fnCheckInputHasValue()
{
var isvalpresent = false;
                        $("div#mydiv").find(":input").each(function () {
                            var type = $(this).attr("type");
                            //alert(type);
                            var $this = $(this);
                            if ($this.is("input")) {
                                if (type == "text") {
                                    var val = $(this).val().trim();
                                    if (val != "") {
                                        isvalpresent = true;
                                    }
                                }
                                //
                                if (type == "checkbox") {
                                    if ($(this).is(":checked")) {
                                        isvalpresent = true;
                                    }
                                }
                                //
                                if (type == "radio") {
                                    if ($(this).is(":checked")) {
                                        isvalpresent = true;
                                    }
                                }
                            }
                            //
                            if ($this.is("select")) {
                                var val = $(this).val().trim();
                                if (val != "" && val != "Select") {
                                    isvalpresent = true;
                                }
                            }
                            //
                            if ($this.is("textarea")) {
                                var val = $(this).val().trim();
                                if (val != "") {
                                    isvalpresent = true;
                                }
                            }
                        });

                        if (isvalpresent == false) {
                            // code
                        }

}
Loading