Use of auto increment in JavaScript and jquery -


i have code

if( !mobilecheck() ) {              this.menu.addeventlistener( 'mouseover', function(ev) {                 self._openmenu();                  document.addeventlistener( self.eventtype, self.bodyclickfn );              } );             this.submenu1.addeventlistener( 'mouseover', function(ev) {                 self._opensubmenu1();                  document.addeventlistener( self.eventtype, self.bodyclickfn );              } );             this.submenu2.addeventlistener( 'mouseover', function(ev) {                 self._opensubmenu2();                  document.addeventlistener( self.eventtype, self.bodyclickfn );              } );         } 

i want able replace

this.submenu1.addeventlistener( 'mouseover', function(ev) {                     self._opensubmenu1();                      document.addeventlistener( self.eventtype, self.bodyclickfn );                  } ); 

with

this.submenu[i].addeventlistener( 'mouseover', function(ev) {                     self._opensubmenu[i]();                      document.addeventlistener( self.eventtype, self.bodyclickfn );                  } ); 

and way simplify code.

i have used

for (var = 0; <= 2; i++) { code above } 

but "typeerror: this.submenu[i] undefined" in firebug.

i beginner in javascript , jquery not easy me.

assuming you're trying avoid repeating code , use [i] index access each variable, can putting jquery objects array instead of individual variables. can replace this:

this.subtrigger = []; this.subtrigger[0] = this.el.queryselector( '.submenu0' ); this.subtrigger[1] = this.el.queryselector( '.submenu1' );     this.subtrigger[2] = this.el.queryselector( '.submenu2' ); 

with this, can access them index.

this.subtrigger = []; (var = 0; <= 2; i++) {     this.subtrigger[i] = this.el.queryselector('.submenu' + i); } 

note: if first object doesn't have "0" suffix, have special case code object.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -