javascript - TypeError: undefined is not a function jQuery -


i'm new jquery , can work, however, reason, when try call function, gives me title error, if in developer tools, works fine.

http://jsfiddle.net/otanan/pmzzlo3e/#&togetherjs=aezijhfbrj

it seems work fine when retrieving classes dom, not when call function such as

.click(function() {}); 

here's code:

var downloads = $(".info"),     classname = "info_clicked";  for(var in downloads) {     downloads[i].click(function()     {         if(!this.hasclass(classname))             this.addclass(classname);         else             this.removeclass(classname);     }); } 

when access jquery collection array, returns dom elements, not jquery objects. should use .each() rather for (i in downloads):

downloads.each(function() {     $(this).click(function() {         if (!$(this).hasclass(classname)) {             $(this).addclass(classname);         } else {             $(this).removeclass(classname);         }     }); }); 

you simplify whole thing to:

downloads.click(function() {     $(this).toggleclass(classname); }); 

most jquery methods automatically iterate on elements in collection if makes sense (the notable exceptions methods return information element, .text() or .val() -- use first element). have iterate explicitly if need different things each element. 1 of great conveniences of using jquery rather plain js: have write explicit iterations.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -