Skip to content Skip to sidebar Skip to footer

Js Getelementsbytagname Except The One With A Specific Class

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 c

Solution 1:

Solution 2:

Try this code:

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

Solution 3:

Try this:

var _inputs = document.getElementsByTagName('input');
var filteredInputs = [];
var re = newRegExp('\\b' + 'nothis' + '\\b');  // or any other class namefor(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

Solution 4:

In modern browsers you can do

var inputs = document.querySelectorAll('input:not(.nothis)');

Post a Comment for "Js Getelementsbytagname Except The One With A Specific Class"