Make Number Change In Descending Order

Asked

Viewed 75 times

0

I want to create an automatic counter, which is marked in what number is every second.

In order for that to happen, I have in mind that he must jump from one element <a> to another every second.

I’m running tests manually, then programming the auto-countdown.

The whole logic is simple, with each click a descent to a previous number

Code

el = document.getElementsByTagName('a')

var x = 0, y = 0;

function voltar()
{
if(y == 6 || x == 0) 
alert("Limite!");
else {
el[x].style.color = "red";
el[y].style.color = "black";
}
x--;
}
a
{ 
text-decoration: none;

cursor: pointer;

padding: 3px; 

color: black; 
}
<a onclick="voltar(x--)" id="menos">Você está no:</a>

<a>1</a>
<a>2</a>
<a>3</a>
<a>4</a>
<a style="color:red">5</a>


Every click in the word You’re in the: then he should go down to the minor number/predecessor

NOTE - the previous color should be restored, leaving in red color only the number highlighted.

1 answer

1


I made some changes with your snippet. Make sure that’s what you want.

el = document.getElementsByTagName('a')

var x = 0;

function voltar(limit)
{
  
  if(x == 0) x = limit;

for(var i = 0; i < el.length; i++){
  el[i].className = "";
}
  el[x].className = "color";
  
  x--;

}
a
{ 
text-decoration: none;

cursor: pointer;

padding: 3px; 

color: black; 
}

.color {
  color:red;
  }
<a onclick="voltar(5)" id="menos">Você está no:</a>


<a>1</a>
<a>2</a>
<a>3</a>
<a>4</a>
<a class="red">5</a>

  • Do not forget to pass as a parameter in the function the number of numbers you have in the anchors. Thus, you have the flexibility to create as many anchors as needed, such as: <a>..</a>&#xA;<a>10</a>&#xA;<a>11</a>&#xA;<a>12</a> In this case, you must pass 12 as the starting parameter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.