I have this variable:
var _inputs = document.getElementsByTagName('input');
But I want to make a new variable that will select all inputs, except the inputs that have a specific class like ".nothis".
var _newinputs = document.getElementsByTagName('input') && document.getElementsByClass('!nothis');
I have this variable:
var _inputs = document.getElementsByTagName('input');
But I want to make a new variable that will select all inputs, except the inputs that have a specific class like ".nothis".
var _newinputs = document.getElementsByTagName('input') && document.getElementsByClass('!nothis');
Share
Improve this question
edited May 4, 2019 at 2:09
Cœur
38.8k26 gold badges205 silver badges277 bronze badges
asked Aug 19, 2011 at 14:23
Adrian FlorescuAdrian Florescu
4,50210 gold badges52 silver badges77 bronze badges
4 Answers
Reset to default 7document.querySelectorAll('input:not(.nothis)')
Try this code:
var _inputs = Array.prototype.filter.call(
document.getElementsByTagName("input"),
function(obj) {
return obj.className.split(" ").indexOf("nothis")===-1;
});
Try this:
var _inputs = document.getElementsByTagName('input');
var filteredInputs = [];
var re = new RegExp('\\b' + 'nothis' + '\\b'); // or any other class name
for(var i=0; i<_inputs.length;i++) {
if (!re.test(input.className)) { // filter out by class name
filteredInputs.push(_inputs[i]);
}
}
Update: Added regex match to eliminate false positives as per suggestion from katspaugh
In modern browsers you can do
var inputs = document.querySelectorAll('input:not(.nothis)');
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743778324a4505642.html
评论列表(0条)