+ 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.

21st Sep 2018, 8:01 PM
Shuaib Nuruddin
Shuaib Nuruddin - avatar
9 odpowiedzi
+ 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.
21st Sep 2018, 8:39 PM
Nimrod A. Holguín L.
Nimrod A. Holguín L. - avatar
+ 5
You're welcome friend. 👍 Your code is very good.
22nd Sep 2018, 3:42 PM
Nimrod A. Holguín L.
Nimrod A. Holguín L. - avatar
+ 5
We will hope the final code. 👍
22nd Sep 2018, 5:39 PM
Nimrod A. Holguín L.
Nimrod A. Holguín L. - avatar
+ 5
Nimrod A. Holguín L. , Calviղ , 🌴Vincent Berger🌴 I am done with my code thanks for the help.
23rd Sep 2018, 9:22 AM
Shuaib Nuruddin
Shuaib Nuruddin - avatar
+ 4
Nimrod A. Holguín L. thanks for the help. Can't believe it was so simple.
22nd Sep 2018, 3:37 PM
Shuaib Nuruddin
Shuaib Nuruddin - avatar
+ 4
Nimrod A. Holguín L. I am busy with the rest of the code.
22nd Sep 2018, 3:44 PM
Shuaib Nuruddin
Shuaib Nuruddin - avatar
+ 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);
21st Sep 2018, 8:20 PM
🌴Vincent Berger🌴
🌴Vincent Berger🌴 - avatar
+ 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
22nd Sep 2018, 2:28 AM
Calviղ
Calviղ - avatar
+ 1
Extra: Here is the list of all Element Objects👍 https://www.w3schools.com/jsref/dom_obj_all.asp
21st Sep 2018, 8:29 PM
🌴Vincent Berger🌴
🌴Vincent Berger🌴 - avatar