+ 2
Changing path d attribute// what's wrong?
what's wrong with this js? https://code.sololearn.com/WQ1Lmx3yjcOw/?ref=app
2 Answers
+ 3
Also, be carreful when mixing simple and double quotes for string litterals (your array declaration have some mistakes of this kind) and use the setAttribute() method rather than 'd' property (which is not related to the 'd' svg element property ^^)... with use of another temporary variable to store partial path ('d' value) for easiest handling it (or clear the 'd' value before starting setInterval() and read actual value with getAttribute() method):
onload = function() {
var robo = document.getElementById("robot");
setInterval(add, 500);
var pth = ['M 60 50 ','h 30 ','v 20 ','h -10 ','v 10 ','h 20 ','v -20 ','h 10 ','v 30 ','h -30 ','v 10 ','h 10 ','v 40 ','h -10 ','v -30 ','h -10 ','v 30 ','h -10 ','v -40 ','h 10 ','v -10 ','h -30 ','v -30 ','h 10 ','v 20 ','h 20 ','v -10 ','h -10 ','Z']
var i = 0;
var d = '';
function add(){
if(i < pth.length){
// robo.d += pth[i];
d += pth[i];
robo.setAttribute('d',d);
++i;
}
else{
clearInterval(add);
}
}
}
+ 6
The document isn't ready. SoloLearn inserts [JS] before </head>, before <body> loads.
window.onload=function(){
// code that needs the DOM, like getElementById();
}