Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
change day by clicking on button

I want that each time I'm clicking on button that I see the Next day from day array list

The only way that I could get an answer is Math.random() to have different result each time I am clicking on button, but I want that show me the next day each time I am clicking on it.

function day() {
  var days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
  document.getElementById("demo").innerHTML = days[Math.floor(Math.random() * 7)];
}
<button onclick='day()'>Click to see</button>
<span id="demo"></span>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could keep an array representing the days in the week while also keep an index to show the current day current.

On each click you will increment current value. The innerHtml value will be the current index (the % is being added to prevent out of bounce)

var days =['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
let current = 0;
function day(){
    document.getElementById("demo").innerHTML = days[current++ % days.length];
}
<button onclick='day()'>Click to see</button>
<div id="demo">

</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

let index = Math.floor(Math.random() *7); //if you want always start from 'Monday' you can use 0 as startIndex
let days =['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
function day(){      
  document.getElementById("demo").innerHTML = days[index];
  index = (index+1)%7     
}
<button onclick='day()'>Click to see</button>
<h1 id="demo"></h1>

about 4 years ago · Juan Pablo Isaza Relatório

0

To start from today you can use getDay which starts on Sunday

const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
let day = new Date().getDay(); //today
document.getElementById('addDay').addEventListener('click', function() {
  document.getElementById("demo").innerHTML = days[day++ % 7]; // move the ++ to ++day to show tomorrow on first click
})
<button id="addDay" type="button">Click to see</button>
<span id="demo"></span>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda