Make Number Switch In Descending Order

0

I want to create an automatic counter, which will check what number it is on every second.

For this to happen, keep in mind that it should jump from one element <a> to another every second.

I am performing tests manually, and then program the self-count.

The whole logic is simple, with each click a descent to an earlier 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 on the word You are in: then it should go down to minor / predecessor

  

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

    
asked by anonymous 29.06.2016 / 16:04

1 answer

1

I made some changes with your snippet. Make sure this is 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>
    
29.06.2016 / 16:14