javascript - How to re-factor 2 functions which are the same? -
i'm working on accessible carousel weak javascript knowledge, , i'm facing syntax problem.
here part gives me problem :
events: function () { var self = this; this.target.on('click', '.nav-right, .right', function (e) { e.preventdefault(); self.movecarousel(self.target.find('.carousel-wrapper li'), 'left', 'right', function (cur, $el) { if (cur === $el.length - 1) { return '0'; } return cur + 1; }); }); this.target.on('focus', '.nav-right, .right', function (e) { e.preventdefault(); self.movecarousel(self.target.find('.carousel-wrapper li'), 'left', 'right', function (cur, $el) { if (cur === $el.length - 1) { return '0'; } return cur + 1; }); });
here full code if that's important (121 lines) : http://jsfiddle.net/12b63z7e/
now, purpose of function ? want slide event fires both on click , on focus. problem is, how wrote it, copy pasting click function focus function, js doesnt fire first function anymore (i cant slide on click now). think need rewrite 2 functions in single function slide item both on focus , on click.
please ask me questions if not clear
you can pass space separated list of event names on() register same handler multiple events on same elements
events: function () { var self = this; this.target.on('click focus', '.nav-right, .right', function (e) { e.preventdefault(); self.movecarousel(self.target.find('.carousel-wrapper li'), 'left', 'right', function (cur, $el) { if (cur === $el.length - 1) { return '0'; } return cur + 1; }); });
Comments
Post a Comment