Cannot read property "removeClass" of Undefined. Javascript -
i trying phone call remove class using call:
myutils.removeclass(e,"empty");
but getting error "cannot read property "removeclass" of undefined" in debugger. can't seem figure out missing removeclass call.
this removeclass function.
var removeclass = function removeclass(el, classstr) { delete el.classname; };
here finish code, html testing 2 methods.
<body> <div id="foo" class="column empty">halloween!</div> <script type="text/javascript" src="utils.js"></script> <script> var e = document.getelementbyid("foo"); var q = function (s){ document.write("<div>"+s+"</div>"); } q("initial (" + e.classname+")"); myutils.removeclass(e,"empty"); q("removed empty (" + e.classname+")"); myutils.addclass(e,"column"); q("added column 1 time again (" + e.classname+")"); myutils.removeclass(e,"scary"); q("removed scary (" + e.classname+")"); myutils.addclass(e,"lum"); q("added lum (" + e.classname+")"); myutils.removeclass(e,"column"); q("removed column (" + e.classname+")"); myutils.addclass(e,"foo"); q("added foo (" + e.classname+")"); myutils.addclass(e,"bar"); q("added bar (" + e.classname+")"); </script> </body>
and myutils function:
var myutils = (function() { function addclass(el, classstr) { if(el.classname !== classstr) { el.classname = classstr; } }; var removeclass = function (el, classstr) { delete el.classname; }; var walkthedom = function walkthedom(el, f) { f(el); el = el.firstchild; while(el) { walkthedom(el,f); el = el.nextsibling; } }; var getelemebtsbyclassname = function getelementsbyclassname(c) { var results = []; var re = new regexp("\\b"+c+"\\b"); var f = function (el) { if(el.nodetype === 1) { if(el.classname.search(re) !== -1) { results.push(el); } } }; walkthedom(document.body, f); }; }());
the problem functions can accessed within myutils
, not outside.
you use
class="lang-js prettyprint-override">var myutils = { addclass: function(el, classstr) { if (el.classname !== classstr) { el.classname = classstr; } }, removeclass: function(el, classstr) { delete el.classname; } /*, ... */ };
or, if want have private variables,
var myutils = (function(){ var thisisprivate = whatever; function addclass(el, classstr) { if (el.classname !== classstr) { el.classname = classstr; } }, function removeclass(el, classstr) { delete el.classname; } /* ... */ // export public properties outside: homecoming {addclass: addclass, removeclass: removeclass/*, ... */}; })();
javascript
No comments:
Post a Comment