Hope anyone could help.
I’ve got a huge amount of paths from svg file (showing only a few here) which I store in an array to dynamically create them on Raphael paper.
var paths = "M539.99,181v95.141h-0.12L509.521,181H539.99zdivideM539.99,276.511v84.85h-30.41L539.99,276.511zdivideM539.99,85.021V181h-30.47L539.99,85.021"; // string with paths from svg file. Much much bigger in real code
var pathsArray = paths.split("divide"); // putting all paths in an array
Everything works until I try to assign an attribute to a path in a onmouseover function inside a for loop. Nothing happens. And no error messages in the console.
var currentPath = window['section' + i];
currentPath.node.onmouseover = function() {
this.style.cursor = 'pointer';
currentPath.attr("fill", "#ccc"); // <-- THIS PART DOESNT WORK
}
I also tried it this way:
window['section' + i].attr("fill", "#ccc");
which gives me an error message:
TypeError: ‘undefined’ is not an object (evaluating ‘window[‘section’ + i].attr’)
Here is the full code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="jquery-1.8.2.js"></script>
<script type="text/javascript" src="raphael.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript">
var paths = "M539.99,181v95.141h-0.12L509.521,181H539.99zdivideM539.99,276.511v84.85h-30.41L539.99,276.511zdivideM539.99,85.021V181h-30.47L539.99,85.021"; // string with paths from svg file. Much much bigger in real code
var pathsArray = paths.split("divide"); // putting all paths in an array
var paper = Raphael(10, 50, 1000, 1000);
for (var i=0;i<pathsArray.length;i++)
{
var currentPath = window['section' + i];
currentPath = paper.path(pathsArray[i]);
currentPath.attr("fill", "#f00");
currentPath.attr("stroke", "#fff");
currentPath.node.onmouseover = function() {
this.style.cursor = 'pointer';
currentPath.attr("fill", "#ccc"); // <-- THIS PART DOESNT WORK
}
}
</script>
</body>
</html>
Try using this keyword, also according to documentation there is a built in mouseover event. So the following should also work:
Even though currentPath was inside the handler function, it was always taking the value of the last ‘currentPath’ in loop.