I’m in the process of creating a timetable. If I click on a
Tables, a pop-up window will open in which the individual subjects are included. If I select an option from there, the pop-up window should close and the selected option should be entered in the table.
HTML
<div class="popup-table">
<div class="popup-window">
<div class="close-popup" onclick="closingPopUp()">
<h4>X</h4>
</div>
<p class="options">Deu</p>
<p class="options">Eng</p>
<p class="options">Mat</p>
<p class="options">Erd</p>
<p class="options">Ges</p>
<p class="options">Mus</p>
<p class="options">Kun</p>
<p class="options">Spo</p>
<p class="options">Che</p>
<p class="options">Phy</p>
<p class="options">Bio</p>
<p class="options">Fra</p>
<p class="options">Spa</p>
<p class="options">Pol</p>
</div>
</div>
<div class="table">
<div class="row headline">
<p>Std</p>
<p>Mo</p>
<p>Di</p>
<p>Mi</p>
<p>Do</p>
<p>Fr</p>
</div>
<div class="row extra">
<p>1</p>
<p id="selectText" onclick="openPopUp()"></p>
<p onclick="openPopUp()"></p>
<p onclick="openPopUp()"></p>
<p onclick="openPopUp()"></p>
<p onclick="openPopUp()"></p>
</div>
<div class="row">
<p>2</p>
<p onclick="openPopUp()"></p>
<p onclick="openPopUp()"></p>
<p onclick="openPopUp()"></p>
<p onclick="openPopUp()"></p>
<p onclick="openPopUp()"></p>
</div>
JavaScript
let row = document.querySelectorAll(".row");
let popUp = document.querySelector(".popup-table");
let closePopUp = document.querySelector(".close-popup");
let options = document.getElementsByClassName("options");
let selectText = document.getElementById("selectText");
function openPopUp() {
popUp.classList.add("show")
}
function closingPopUp() {
popUp.classList.remove("show")
}
for(option of options){
option.onclick = function(){
selectText.innerHTML = this.textContent;
closingPopUp();
}
}
The whole thing only works if I assign an Id and not on a Class. But I can only change this one table field.
Maybe someone can help me make this work and I can make a selection individually in each table field.
2
Answers
To make your code work for multiple table fields, you can use the
event.target
property to get the element that was clicked on. You can then use theid
orclass
of that element to determine which table field to update.Here is a modified version of your code that works for multiple table fields:
This code works by passing the element that was clicked on to the
openPopUp()
function. TheopenPopUp()
function stores this element in a property calledclickedElement
. When the user clicks on an option in the pop-up window, theclosingPopUp()
function is called. This function updates the innerHTML of theclickedElement
with the text of the option that was clicked on. It then closes the pop-up window.You can use this same approach to update any table field, regardless of whether it has an ID or a class.
You could cache the currently active element, please see the example: