JS Uncaught TypeError: Cannot read property ‘style’ of undefined(…)

Original code:

                        for(var i=0; i<li_obj.length; i++) {
				li_obj[i].onmouseover = function() {
					 li_obj[i].style.backgroundColor = "blue"; 
				}
			} 

Error: Uncaught TypeError: Cannot read property ‘style’ of undefined(…)

How to Solve this error:

                        for(var i=0; i<li_obj.length; i++) {
				li_obj[i].index = i;
				li_obj[i].onmouseover = function() {
					 li_obj[this.index].style.backgroundColor = "blue"; 
				}
			}

Or

                        for(var i=0; i<li_obj.length; i++) {
				li_obj[i].onmouseover = function() {
					 this.style.backgroundColor = "blue"; 
				}
			}

Similar Posts: