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:
- JS Uncaught TypeError: Cannot read property ‘toLowerCase’ of undefined
- Electronic error: Uncaught TypeError: Cannot read property ‘dialog’ of undefined
- Vue: How to use Vue-router in Axios
- WPF TextBox Placeholder
- [How to Solve] Uncaught ReferenceError: app is not defined
- [Solved] Vue route jump error: Uncaught TypeError: Cannot read property ‘push’ of undefined
- Cannot read property ‘length’ of undefined
- Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method
- How to Customizate the Style of Option Box by JQuery Component
- [Solved] TypeError: Cannot read property ‘getAttribute’ of undefined