+ 1

Can't select element (JavaScript)

I'm trying to make a project, it's porpuse is to basically update the navigation bar of a website I have to all pages at once every time I want to add a new link, so I don't have to go and add it in every page myself, and I have done this fine so far. I now however wanted to create another code, which code I want to interact with the navigation bar I created before, and it just doesn't work, it doesn't find the elements, I wanted to make it so that the link of the page you are currently viewing is replaced by a paragraph with an underline, it should be simple, but for some reason it doesn't work, I think it doesn't find the element because the code runs before the elements are created, but I'm not sure how to fix that, can anyone help? Here is the code: https://code.sololearn.com/WOQ27nkJ7alz/?ref=app

7th Oct 2020, 7:07 PM
Karak10
Karak10 - avatar
5 Answers
+ 8
If that is truly the issue with the code, then perhaps you could try putting all the Js isnide this function window.onload = function(){ //All code goes here } or in the HTML, add the defer attribute to the script tag like theirs <script src="main.js" defer </script> Either of those methods make sure the scripts are loaded AFTER the HTML! Hope this helps and have a great day â˜•đŸ©!
7th Oct 2020, 7:25 PM
Vachila64☕
Vachila64☕ - avatar
+ 8
Wow, nicely done Karak10 I was thinking about going the same route, but with a custom attribute, like perhaps pageNumber, then using Js to look for that attribute and do accordingly to the number gotten In the attribute, but id is also a good way to go, thumbs up👍, sorry I took time to get back to you, was working on an answer to a question someine asked me earlier 😅 Again, nice work!👍
7th Oct 2020, 8:41 PM
Vachila64☕
Vachila64☕ - avatar
+ 7
I'm going through the code now, let me see what I can come up with, I'm not certain tho but I'll try😂😅
7th Oct 2020, 8:10 PM
Vachila64☕
Vachila64☕ - avatar
+ 2
Vachila64☕ I found a solution, it's probably not the best but it works I guess, I will check the id of nav to check which page is which and in page one nav will have an id of one, in page two it will have an id of two, etc, here is the code https://code.sololearn.com/Wi4FD5O97YWI/?ref=app
7th Oct 2020, 8:36 PM
Karak10
Karak10 - avatar
0
Vachila64☕ I can't put all the code together for one reason, I don't know how can I make it check which page is which so I can give different instructions for each page, I want the code that creates the links to be inserted from an external file so that whenever I add a new link it is added in all pages automatically. I then wanted to make it so the link of the viewed page gets replaced with a paragraph with an underlined text, since I don't know how to check which page is which I decided to just write this code in each page separately, which wouldn't take much time since I would just create a function and all I would've have to do then would be to copy paste it in all pages and change the two parameters according to the page.
7th Oct 2020, 7:48 PM
Karak10
Karak10 - avatar