J.S. arrays help - | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 2

J.S. arrays help -

var x = [doc.get...ById("elm"), doc.get...ById("elm2")] ; x[0][1].style.display="none"; That' not working. How do you make them display none by using the x.[] ? Here's started code: https://code.sololearn.com/WB0NX5uyRS7t/?ref=app

17th Jun 2019, 8:18 PM
Ginfio
Ginfio - avatar
6 Antworten
+ 3
https://code.sololearn.com/WR8n8y2WF1Bs/?ref=app
17th Jun 2019, 11:51 PM
Calviղ
Calviղ - avatar
+ 3
Your array isn't multidimensional. I get why you are confused, but getElementById doesn't return an array of elements, it only returns one, unlike getElementsByClassName or getElementsByTagName. You should just remove the last part of the array: x[1].style.display = "none"; Side note: You get the elements before the document is loaded, therefore it will be null. To fix this, you should wrap this code in the window.onload function. Make sure you remove the var keyword
17th Jun 2019, 8:34 PM
Airree
Airree - avatar
+ 3
If you are sure that you will only have 2 elements, you can just write both lines, otherwise you should use an (enhanced) for loop
17th Jun 2019, 8:42 PM
Airree
Airree - avatar
+ 2
There are 2 issues I found: The first is that because you're loading in the JS before the HTML, there are no elements in the DOM to be manipulated, raising an error as a result of the JS not being able to find the object with the id's specified. To avoid this, you could wrap everything within: window.onload = function(){ } to get it to run once the HTML has loaded in. The second issue (as Airree stated) is that you seem to be trying to find an element at an index that doesn't exist. Rather than trying to use x[0][1] to access the elements, all you would need to do is use x[i], where i is the index of the object you don't want to display (being either 1 or 0 in this case)
17th Jun 2019, 8:34 PM
Faisal
Faisal - avatar
+ 2
ok, so i can understand what u guys are saying a little more, can you access both ids (with array x[]), and display both ' a' and ' b ' to none? https://code.sololearn.com/WB0NX5uyRS7t/?ref=appa Thx!
17th Jun 2019, 8:40 PM
Ginfio
Ginfio - avatar