how to use jquery to select all inputs, that are not submit, reset or button?
Solution 1
Try modifying your selector to chain .not(...)
like:
var inputs = $('input, textarea, select')
.not(':input[type=button], :input[type=submit], :input[type=reset]');
$(inputs).each(function() {
console.log(this.type);
});
This makes it (arguably) easier to read, and should work how you expect.
Solution 2
This answer is arguably harder to read than Michael Robinson's, but I find it more succinct. Rather than run the not()
function, you can select for it using :input
to gather all form elements, then filter with the :not()
selector for each unwanted input type.
From jQuery's API documentation (https://api.jquery.com/input-selector/):
The
:input
selector basically selects all form controls.
The "CSS"-like selector version, the :not()
selectors are separate, and can be used in CSS 3, too:
var inputs = $("#theForm").find("input:not([type=button]):not([type=submit]):not([type=reset])");
Related videos on Youtube
frequent
Updated on July 09, 2022Comments
-
frequent almost 2 years
I'm trying to select all input elements except input type="submit/reset/button
I have tried to make a selector like this:
inputSelector = 'input:not(input[type=button], input[type=submit], input[type=reset]), textarea, select';
But this does not work, because the submit buttons always make into the final selection.
Any idea what's wrong with the above.
Thanks!
-
frequent about 12 yearsI'm using it in a plugin, so I guess I'm stuck with the crummy selector. still trying...