/    Sign up×
Community /Pin to ProfileBookmark

can’t make links active when scrolling to a specific section in page

Hi, So I’m learning javascript and I’m stuck in a function here. I’m doing a landing page functionality with pure javascript and not allowed to use any libraries. I want to make a link on scroll to a specific section to be active. so I did a helper function IsInViewPort() to sense the position when I’m scrolling the page. I’m trying to make a function that is on scrolling, Looping inside the sections in the page to add the specified section to the classList and make its link from navbar active. when I added this function there was an error says “unexpected identifier” and the navbar disappeared. how to fix it?
the function with the issue:
`window.addEventListener(“scroll” , e=>{
for(let i=1 ; i<=sections.length;i++){
if(isInViewport(sections[i])){
sections[i].classList.add(“your-active-class”);
document.getElementsByClassName(`
${sections[i].id}`)[0].classList.add(“active”);
}
else{
section.classList.remove(“your-active-class”);
document.getElementsByClassName(`
${sections[i].id}`)[0].classList.remove(“active”);
}
}`

the codepen with the full code:
https://jsfiddle.net/dode2/yqne0orj/1/

I appreciate your help. I need help asap.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumFeb 21.2020 — There are two errors in this line:

In your posting: `for(let i=1 ; i&lt;=sections.length;i++){</C><br/>
In your fiddle: <C>
for(int i=0 ; i&lt;=sections.length;i++){</C>

<C>
let</C> is correct, <C>int</C> is not.<br/>
You have to start the loop at 0 and the end condition should read <C>
i&lt;sections.length</C>

The complete line: <C>
for(let i=0 ; i&lt;sections.length;i++){`
Copy linkTweet thisAlerts:
@dodeauthorFeb 21.2020 — @Sempervivum#1615028

I iterate it the loop but when I tested it, it gives me 200+ errors each time I scroll. what should I do? 😭
Copy linkTweet thisAlerts:
@dodeauthorFeb 21.2020 — @Sempervivum#1615028

Is the function logic wrong ? or what am I doing wrong?
Copy linkTweet thisAlerts:
@SempervivumFeb 21.2020 — 200+ errors are bad news on one hand but good news on the other: The loop is correct now, but there is another error. Going to look into ...
Copy linkTweet thisAlerts:
@dodeauthorFeb 21.2020 — @Sempervivum#1615032

Okay. I just want to say thank you for helping me 🙋
Copy linkTweet thisAlerts:
@SempervivumFeb 21.2020 — When I change the loop to this:
// Set sections as active
window.addEventListener("scroll" , e=&gt;{
for(let i=0 ; i&lt;sections.length;i++){
if(isInViewport(sections[i])){
sections[i].classList.add("your-active-class");
}
else{
sections[i].classList.remove("your-active-class");
}
}
});

there are no errors left in the console.

Now you can continue debugging.
×

Success!

Help @dode spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...