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');

4 Answers

6
katspaugh On Best Solutions
3
Mrchief On

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

1
Gustav Barkefors On

In modern browsers you can do

var inputs = document.querySelectorAll('input:not(.nothis)');
3
Andrew D. On

Try this code:

var _inputs = Array.prototype.filter.call(
                   document.getElementsByTagName("input"),
                   function(obj) { 
                     return obj.className.split(" ").indexOf("nothis")===-1;
                   });