Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

86
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda