jquery - Javascript stops working on entering multiple class name -
i simple problem here javascript. have js code use highlight rows in price table. issue if used multiple class names @ time stops working. example:
<div class="el1 someclass">hover not work in this</div>
<div class="el1">hover work in this</div>
the js:
var classes = ["el1", "el2", "el3", "el4", "el5", "el6", "el7", "el8", "el9","el10","el11","el12", "el13","el14","el15","el16","el17","el18","el19","el20","el21","el22", "el23", "el24","el25" ]; //list of classes var elms = {}; var n = {}, nclasses = classes.length; function changecolor(classname, color) { var curn = n[classname]; for(var = 0; < curn; ++) { elms[classname][i].style.backgroundcolor = color; } } for(var k = 0; k < nclasses; k ++) { var curclass = classes[k]; elms[curclass] = document.getelementsbyclassname(curclass); n[curclass] = elms[curclass].length; var curn = n[curclass]; for(var = 0; < curn; ++) { elms[curclass][i].onmouseover = function() { changecolor(this.classname, "#dbdbdb"); }; elms[curclass][i].onmouseout = function() { changecolor(this.classname, "transparent"); }; } };
can me out please, i'm newbie in js.
thanks ..
the classname
property on dom element returns complete classname; is, if element has multiple classes (e.g. <div class="herp derp"></div>
), classname = "herp derp"
.
if want call changecolor
on each of these multiple classes, try this:
// first, change order of arguments on changecolor function can pre-apply color argument function changecolor(color, classname) { var curn = n[classname]; for(var = 0; < curn; ++) { elms[classname][i].style.backgroundcolor = color; } }
now, can change how call changecolor
// now, called changecolor, this.classname.split(" ").foreach(changecolor.bind(null, "transparent")); // call changecolor on of classes of appropriate color ("transparent" or "#dbdbdb")
Comments
Post a Comment