Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7952291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:49:04+00:00 2026-06-04T02:49:04+00:00

I have some links and want to pass the focus one by one automatically.

  • 0

I have some links and want to pass the focus one by one automatically.
I’m using the code below. Works fine passing the focus but only once.
The first time the focus pass, the link change the color, then pass to the next. The one before gets black again and the next is painted red. No problem.
But when its reach the final link doesn’t restart.

var i = 0;
var letras = document.getElementsByTagName("a");

function pasaLink() {
    if (i == 0) {
        letras[letras.length-1].style.color = "black";
    } else {
        letras[i-1].style.color = "black";
    }
    letras[i].style.color = "red";
    letras[i].focus();
    i++;
    if (i > letras.length) {
        i= 0;
    }
    setTimeout("pasaLink()",2000);
}

With the final if and i=0; I’m trying to get back to the initial index when reach the final element, and then restart to iterate the entire collection of links in letras.
This function is called in the body.onload().

Any ideas why its not working?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:49:06+00:00Added an answer on June 4, 2026 at 2:49 am

    Change your if to:

    if (i >= letras.length) {   
       i= 0;     
    } 
    

    Working example:
    http://jsfiddle.net/nivas/Nn62Q/
    (click the Orig Button to see the behavior of your code and the New button to see the behavior of the new one. I have added a try...catch to show the error)

    Explanation:
    Let us say there are five a tags.
    You make i = 0 when i > 6 (i.e., 6). But when i becomes 5, letras[i].style.color = "red"; at the beginning of the function fails and an exception is throws a exception. The flow stops here so the if (i > letras.length) will never be reached.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code to get my data using a min_row/max_row. I want to
I have some text that has HTML hyper-links in it. I want to remove
I have some jQuery code that intercepts links clicked on a page: $(document).ready(function() {
I have got some code to pass in a variable into a script from
I have some data Foo that I want to pass from the browser to
I have some text which I want to have on separate lines. I try
I have some html files and want to extract only lines with containing these
I have some textboxes on a page and I want to click a link
suppose I have this string: some striinnngggg <a href=something/some_number>linkk</a> soooo <a href=someotherthing/not_number>asdfsadf</a> I want
I have textarea in my site(link shortening site) i want to user enter some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.