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

85
Visualizações
Add clicks when differents div are clicked

Im working on a project and i have basically some troubles with things for my website. This one is a bit hard for me, i have some ideas but i dont know how to do them in my javascript code. I have 98 divs (it's a calendar where you can select everyday differents hours to book slots). There is a Summary (kinda same thing on commercial website) which i want that it says how many slots you selected. But the problem is that i have like I said 98div so i wanna do it in one function. On the slots you want to book, you can click on it (it selects it) and if you click on it again it deselects it. I want that you can select as many slots that you want, and the summary shows how many you selected then you can go to next step. Here is my code if you guys have some ideas !

function x1(e) {
var target = e.target,
count = +target.dataset.count;


 target.style.backgroundColor = count === 1 ? "#707070" : 'black';  
 target.dataset.count = count === 1 ? 0 : 1;
 target.innerHTML = count === 1 ? '' : 'réserver';
 target.classList.toggle('Resatxt');
 target.classList.toggle('unselectable');
 
 }

Actually this code is for the selection of the slots (it's going background black when you clicl on it, and then back to the normal color when you deselect it). But i think i can do what i want with this. I thinked about incrementing +1 when we click on the div but the problem that i dont know how to figure it out is when you deselect it you have to do -1 but im a bit lost.

I tried to be clear but ik that is not really. If you guys have some ideas, go for it. Thanks a lot

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

0

it's nice to see that your joining the programming community. I hope I understood you correctly and made a simple and minimal example to present you how can you achieve what you want. This is just an idea, don't take it too serious and write your own logic to handle the functionality!

const divs = 98;
const list = document.querySelector("#list");
const selectedList = document.querySelector("#selectedList");
let selected = [];
let elementsAdded = 1;

const onSelectDiv = (element) => {
  const elementCopy = element.cloneNode(true);
  elementCopy.id += "-copy";

  selected = [
    ...selected,
    {
      id: elementsAdded,
      elementId: element.id
    }
  ];

  elementsAdded += 1;
  selectedList.appendChild(elementCopy);
};

const onClick = (e) => {
  if (e.target.className.includes("selected")) {
    e.target.classList.remove("selected");
    elementsAdded -= 1;

    const elementToDelete = selected.findIndex(
      (x) => e.target.id === x.elementId
    );
    
    selectedList.removeChild(selectedList.childNodes[elementToDelete + 1]);

    selected = selected.filter((x) => x.elementId !== e.target.id);
    return;
  }

  onSelectDiv(e.target);
  e.target.className += " selected";
};

for (let i = 0; i < divs; i++) {
  const div = document.createElement("div");
  div.innerHTML += i;
  div.className += "div";
  div.id = i;
  div.addEventListener("click", function (event) {
    onClick(event);
  });

  list.appendChild(div);
}
.view {
  display: flex;
  flex-direction: 'column';
}
#list {
  display: flex;
  width: 400px;
  max-width: 500px;
  flex-wrap: wrap;
}

.div {
  padding: 5px;
  background-color: black;
  cursor: pointer;
  color: white;
  border-radius: 10px;
  margin: 10px;
}

.selected {
  background-color: red;
  color: white;
}
<div class="view">
<div>
  <p>Elements:</p>
  <div id="list">
  </div>
</div>
<div>
  <p>Selected:</p>
  <div id="selectedList">
  </div>
</div>
</div>

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