jquery - Javascript multiple class each function get selected class name -
<div class="myclass effect1">bla bla</div> <div class="myclass effect2">bla bla</div> <div class="myclass effect3">bla bla</div> <div class="myclass effect4">bla bla</div> jquery('.effect1, .effect2, .effect3, effect4').each( function() { //i need find class chosen?? var newclass= chosen class here here jquery(this).addclass('animated'); } ); i've added information inside code above. how can make work? in advance
within iterator function, this matching dom element. element has classname property space-delimited list of classes on element. there may more one, unless know in markup there one.
if one, use this.classname directly.
var newclass = this.classname; if more one, may find jquery's hasclass function useful:
var $this = $(this); if ($this.hasclass('effect1')) { // ... } else if ($this.hasclass('effect2') { // ... } // ... but, if want different things depending on class element matched, makes more sense break query. may not need each, if you're trying elements matching given class.
for example:
jquery(".effect1").animate({/*..settings effect1..*/); jquery(".effect2").animate({/*..settings effect2..*/); jquery(".effect3").animate({/*..settings effect3..*/); jquery(".effect4").animate({/*..settings effect4..*/); from comment, sounds might want dispatch map, this:
var map = { effect1: function() { /* ...stuff effect1... */ }, effect2: function() { /* ...stuff effect2... */ }, // ...and on... }; you put on main level of code.
within each (if want use each), can identify first effectx class in this.classname this:
var match = /effect\d/.exec(this.classname); var cls = match && match[0]; and can use dispatch map it:
if (cls) { map[cls].call(this); } that calls function, making this within function dom element. or pass argument instead:
if (cls) { map[cls](this); } ...and have dispatch functions accept it, e.g. effect1: function(element) {...
Comments
Post a Comment