I did a bit of Googling, and I found that the following code does not work because inputs[i].className is not a string. How would I make this a string??? I tried toString(). Also, inputs.length is not 0. I checked.
for (var i = 0; i < inputs.length; i++)
{
if (inputs[i].className.indexOf("blah") == 1)
{
//do something
}
}
I want to do something with only the inputs array values that have a class name of something like “blah 1;2;3;4”.
Any help would be appreciated.
indexOf(value)returns-1when no match, and a index starting with0when there is match.In your case, you should compare the result with 0, instead of 1.
Note that, if you want to test if a node has some certain class name, this is not a good practice.
A better approach is to use DOM API
node.classList.contains()which is available in modern browsers.Or use regular expression
/\bblah\b/.test(node.className)to avoid the cases thatblahis a substring of another class name, saynot-blah.Another way is to use mootools, which provides
node.hasClass()toHTMLElementinstances.Or use jQuery like
$(node).hasClass().