Attempting a dynamic style change below, but the function falls silent. I’ve used JSHint, and all seems fine.
function styleInput(elClass,color)
{
"use strict";
var document;
elClass = document.getElementsByClassName(elClass);
var len = elClass.length;
for (var i=0; i < len;i++) {
if (elClass[i].className === elClass)
{
var tmp = elClass[i];
tmp.style.backgroundColor = color;
tmp = " ";
}
}
}
styleInput("fm_bushido", "#f3f3f3");
—–HTML—–
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<input type="text" name="name" class="fm_bushido txt" placeholder="Name:"></input>
</body>
</html>
You’re using the variable
elClasstwice and overwriting the value passed into the function with that returned fromdocument.getElementsByClassName(elClass);. Once your script gets down toif (elClass[i].className === elClass), you’re comparing the classname of the element with the element itself, not the value you passed into the function (fm_bushido).Just change one of those elements’ names and it should work.