+ 3
Why is my code not working.
I have this code and I want to change the colour of the circle and background using dom but its not working. Does dom work different with svg because its from xml? https://code.sololearn.com/WDVFWdpQkb3U/?ref=app Edit: I managed to fix my problem. Thanks to everyone who helped. I am done with the rest of my code.
9 ответов
+ 5
The solution is charging the js after the document has charged:
window.onload = function(){//You need this sentence.
var rectTag = document.getElementsByTagName("rect");
var circleTag = document.getElementsByTagName("circle");
function day() {
rectTag[0].setAttribute("fill","#69f");//I also tried rectTag[0].fill = "#69f" and its not working
circleTag[5].setAttribute("fill","yellow");
setTimeout(night,10000);
}
function night() {
rectTag[0].setAttribute("fill","#1a001a");
circleTag[5].setAttribute("fill","#e6ffff");
setTimeout(day,10000);
}
day ();
}//Don't forget this.
+ 5
You're welcome friend. 👍 Your code is very good.
+ 5
We will hope the final code. 👍
+ 5
+ 4
Nimrod A. Holguín L. thanks for the help. Can't believe it was so simple.
+ 4
Nimrod A. Holguín L. I am busy with the rest of the code.
+ 2
Hi What does the JUMP_LINK__&&__Python__&&__JUMP_LINK say?,
your setAttribute does not has the correct syntax. Best way is to create a basic attribute like id="" or class="" to work with DOM JS in my experience
Hope this helps👍
element.setAttribute("AttributeName", "Attributevalue");
rectTag.setAttribute = ("class", "rect");
// Create a class named rect:
.rect {
backgroundcolor:red;
}
https://www.w3schools.com/jsref/met_element_setattribute.asp
Additional::
To make the code shorter an work with css i recommend u use Jquery & Jquery UI.
$("#id_or_.class").stop().animate({backgroundColor : "red"},2500);
+ 2
What does the JUMP_LINK__&&__Python__&&__JUMP_LINK say?
Your code on DOM access is working, dont change it.
You just called day() in wrong timing, before DOM elements loaded.
Just change day() to
onload = day()
It would be working fine. :)
https://code.sololearn.com/WHcixtXQO5pX/?ref=app
+ 1
Extra: Here is the list of all Element Objects👍
https://www.w3schools.com/jsref/dom_obj_all.asp