AARRGGHHH Posted September 26, 2022 Share Posted September 26, 2022 for (i = 1; i = 5; i++) { document.getElementById("" + i + "").style.display = "none"; } Error: Uncaught TypeError: Cannot read properties of null (reading 'style') I'm going to have to write out all 5 lines of code because the loop won't work. So that this does not happen again, I'd appreciate if someone could tell me what rookie mistake I made. The quotes don't look right but nothing I tried fixed the error. Thank you! Link to comment Share on other sites More sharing options...
Ingolme Posted September 26, 2022 Share Posted September 26, 2022 Just check the tutorial page again: https://www.w3schools.com/js/js_loop_for.asp This code is trying to access HTML elements. You must make sure that this code doesn't run until after those elements have been loaded, otherwise it won't find them. Link to comment Share on other sites More sharing options...
AARRGGHHH Posted September 26, 2022 Author Share Posted September 26, 2022 52 minutes ago, Ingolme said: Just check the tutorial page again: https://www.w3schools.com/js/js_loop_for.asp This code is trying to access HTML elements. You must make sure that this code doesn't run until after those elements have been loaded, otherwise it won't find them. I don't recall ever having a problem with that. Are you saying that this code should be in the <body> and not the <head>? Are the quotes correct? Thank you very much Link to comment Share on other sites More sharing options...
dsonesuk Posted September 26, 2022 Share Posted September 26, 2022 <!DOCTYPE html> <html> <head> <style> #demo { background-color: yellow; } [id^=demox] { background-color: lime; } </style> </head> <body> <h2>JavaScript For Loop</h2> <p id="demo"></p> <p id="demox1"></p> <p id="demox2"></p> <p id="demox3"></p> <p id="demox5"></p> <p id="demox4"></p> <p id="demox0"></p> <p id="demox6"></p> <p id="demox7"></p> <script> const cars = ["BMW", "Volvo", "Saab", "Ford", "Fiat", "Audi"]; let text = ""; let text2 = ""; for (let i = 0; i <= 5; i++) { //for multiple in single paragraph text += cars[i] + " xx<br>"; //single value from set id ref var thisDemo = document.getElementById("demox5") //multiple values from id and index ref //var thisDemo = document.getElementById("demox"+i) if(thisDemo){ //if exists text2 = cars[i] + " yy"; thisDemo.innerHTML = text2; } } document.getElementById("demo").innerHTML = text; </script> </body> </html> First you shouldn't use number at the beginning of id. Second, you should check if id even exists if looping through index's Third you should set a value to restrict the for index loop i < 5; else it will go into a infinite loop with I++; Link to comment Share on other sites More sharing options...
AARRGGHHH Posted September 26, 2022 Author Share Posted September 26, 2022 5 hours ago, dsonesuk said: Second, you should check if id even exists if looping through index's I appreciate your reply. I didn't quite catch what you meant by the above, can I ask you to clarify "looping through index's"? In regard to my "syntax error", if I understand correctly, the syntax and quotes are fine, the problem is the placement of the script in the "<head></head> area, correct? Thank you very much Link to comment Share on other sites More sharing options...
dsonesuk Posted September 27, 2022 Share Posted September 27, 2022 The page is read from top to bottom the element/s in this case with id beginning with 'demox' will be a collection, the index will be i 1 to 5 in each loop. These referred to in javascript must exist for it to be processed in the for loop, so it must be placed after the elements referred too. Link to comment Share on other sites More sharing options...
AARRGGHHH Posted September 27, 2022 Author Share Posted September 27, 2022 Okay, thank you very much On 9/25/2022 at 10:38 PM, AARRGGHHH said: for (i = 1; i < 6; i++) { document.getElementById("" + i + "").style.display = "none"; } The quotes in the parenthesis, is this correct? Link to comment Share on other sites More sharing options...
dsonesuk Posted September 27, 2022 Share Posted September 27, 2022 Why? Are they used? They don't do anything because there empty. As i said a id must not be a number on its own or be at beginning of id ref, so using that to add text at begginnig would correct that! but the end quotes are not required. Link to comment Share on other sites More sharing options...
AARRGGHHH Posted October 7, 2022 Author Share Posted October 7, 2022 Okay, thank you! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now