How to check if the DOM has any visible input forms, e.g. input field, select field, multitext field, etc. there is atleast one of those, I will need to run a开发者_开发知识库 certain code. If all are hidden not atleast 1 is shown I will not show run the code.
if(ThereIsVisibleInputFields) {
IWillRunThisCode();
}
EDIT:
How about using this.
if($(":input:visible").length) {
IWillRunThisCode();
}
Does :input
selector selects all form elements?
The :visible selector is what you most likely need. You can use it as such-
if ($('input:visible, textarea:visible, select:visible').length > 0) {
IWillRunThisCode();
}
You can check the jQuery api page for more info - :visible Selector
Edit: As pointed out by OP, the :input selector works equally well. Even better, it also selects buttons, which my original code didn't. So the selector should really be
if ($(':input:visible').length) {
IWillRunThisCode();
}
function ThereIsVisibleInputFields(){
var arr = new Array();
arr = document.getElementsByTagName( "input" );
for(var i=0; i < arr.length; i++)
{
var tagName = document.getElementsByTagName( "*" ).item(i).nodeName;
var tagObj = document.getElementsByTagName( "*" ).item(i);
if(tagObj.style.visibility=="visible"||tagObj.style.display!="none")
return true;
}
return false;
}
Similarly we can chek for other tags also(select,textarea etc)
精彩评论