Reputation: 749
Is there any way to check if a specific form has any input fields in it? Or if it has no input fields?
Update:
Also how do I return the focus to a button on page load if the condition is met? The below doesn't seem to work
var numInputs = $("#forgotPassword").find("input:visible").length;
if(numInputs == 0){
$('.button-class > a').focus();
}
Upvotes: 2
Views: 544
Reputation: 83376
I would just select all the inputs inside the form and check to see how many come back
var numInputs = $("#formId input").length;
if (numInputs === 0)
console.log("no inputs");
And of course if you'd like to include textareas and selects
var numInputs = $("#formId input, #formId textarea, #formId select").length;
or more simply:
var numInputs = $("#formId").find("input, textarea, select").length;
EDIT
To exclude hidden inputs, you can use the :visible
pseudoselector
var numInputs = $("#formId input:visible").length;
Upvotes: 4
Reputation: 44215
Just check if the selector result contains any elements (here I assume that by input field you mean text input fields):
$('#myform').find('input[type="text"]').length
Upvotes: 2