Starting with the initial code, inline HTML event attributes are bad, so move those down to actual JavaScript code.
Comments show code before the changes
<!--<div class="box" onclick="changeText(this); getDate(arrival1, 2019-12-01);">-->
<div class="box">
1
</div>
<!--<div class="box" onclick="changeText(this); getDate(arrival2, 2019-12-02);">-->
<div class="box">
2
</div>
const boxes = document.querySelectorAll(".box");
boxes[0].addEventListener("click", function (evt) {
changeText(this); getDate(arrival1, 2019-12-01);
});
boxes[1].addEventListener("click", function (evt) {
changeText(this); getDate(arrival2, 2019-12-02);
});
Letās now run the JavaScript code through JSLint to help reveal issues.
Unexpected ā1ā after ā0ā.
Thatās because the 2019-12-01
needs to be in quotes.
// changeText(this); getDate(arrival1, 2019-12-01);
changeText(this); getDate(arrival1, "2019-12-01");
Unexpected ā2ā after ā0ā.
Same issue, different line.
// changeText(this); getDate(arrival2, 2019-12-02);
changeText(this); getDate(arrival2, "2019-12-02");
Undeclared ādocumentā.
We can tell JSLint that we expect this code to be run in a browser, by adding the following declaration to the top of the JavaScript code:
/*jslint browser */
Unexpected āthisā.
The this keyword can cause confusion. We can get the element from the event target instead.
const el = evt.target;
// changeText(this); getDate(arrival1, "2019-12-01");
changeText(el); getDate(arrival1, "2019-12-01");
Undeclared āarrival1ā.
That arrival1 variable is supposed to rever to the div with an id attribute of āarrivalā. We can use a local variable to store that.
const arrival = document.querySelector("#arrival1");
// changeText(el); getDate(arrival1, "2019-12-01");
changeText(el); getDate(arrival, "2019-12-01");
Unexpected āthisā.
We then have duplicate issues with box 2, so we can copy/paste the code and change some 1ās to 2ās.
boxes[0].addEventListener("click", function (evt) {
const el = evt.target;
// const arrival = document.querySelector("#arrival1");
const arrival = document.querySelector("#arrival2");
// changeText(el); getDate(arrival, "2019-12-01");
changeText(el); getDate(arrival, "2019-12-02");
});
Make similar functions the same
That copy/paste tells me that we can do this in a simpler way. Iāll use an index variable and get the numbers from that instead.
const boxes = document.querySelectorAll(".box");
let index = 0;
boxes[index].addEventListener("click", function (evt) {
const el = evt.target;
const day = index + 1;
// const arrival = document.querySelector("#arrival1");
const arrival = document.querySelector("#arrival" + day);
// changeText(el); getDate(arrival, "2019-12-01");
changeText(el); getDate(arrival, "2019-12-0" + day);
});
index += 1;
boxes[index].addEventListener("click", function (evt) {
const el = evt.target;
// const arrival = document.querySelector("#arrival2");
const arrival = document.querySelector("#arrival" + day);
// changeText(el); getDate(arrival, "2019-12-02");
changeText(el); getDate(arrival, "2019-12-0" + day);
});
Turn into a loop
We can now use that function code as we loop through each of the boxes:
const boxes = document.querySelectorAll(".box");
boxes.forEach(function (box, index) {
box.addEventListener("click", function (evt) {
// index += 1;
const el = evt.target;
const day = index + 1;
const arrival = document.querySelector("#arrival" + day);
changeText(el); getDate(arrival, "2019-12-0" + day);
});
// index += 1;
// boxes[index].addEventListener("click", function (evt) {
// const el = evt.target;
// const arrival = document.querySelector("#arrival" + day);
// changeText(el); getDate(arrival, "2019-12-0" + day);
// });
});
Expected āidā at column 4, not column 2.
Thereās more to fix up. First, indenting with multiples of 4 spaces.
function changeText(id) {
id.classList.toggle("box-color");
}
function getDate(arrival, theDate) {
document.getElementById("arrival").innerHTML="theDate";
}
Also, the changeText function uses id for a function parameter, but itās using classList on an element instead. Itās time to rename that function parameter to el.
// function changeText(id) {
function changeText(el) {
// id.classList.toggle("box-color");
el.classList.toggle("box-color");
}
Unused āarrivalā.
The getDate function doesnāt need to get the element, as weāve already done that in the event function. We can just use the arrival element that weāve given to the getDate function.
function getDate(arrival, theDate) {
// document.getElementById("arrival").innerHTML="theDate";
arrival.innerHTML="theDate";
}
Unused ātheDateā.
The ātheDateā shouldnāt be in quotes.
function getDate(arrival, theDate) {
// arrival.innerHTML="theDate";
arrival.innerHTML = theDate;
}
Also, based on the innerHTML, we are not getting the date, we are setting the date instead. We should rename the function so that it is more consistent with what it actually does.
// function getDate(arrival, theDate) {
function setDate(arrival, theDate) {
arrival.innerHTML="theDate";
}
...
changeText(el);
// getDate(arrival, "2019-12-0" + day);
setDate(arrival, "2019-12-0" + day);
Expected āsetDateā at column 8, not column 24.
You really shouldnāt have multiple statements on the same line either, so weāll put them on separate lines:
// changeText(el); setDate(arrival, "2019-12-0" + day);
changeText(el);
setDate(arrival, "2019-12-0" + day);
Things now work
And the code now finally works.
function changeText(el) {
el.classList.toggle("box-color");
}
function setDate(arrival, theDate) {
arrival.innerHTML = theDate;
}
const boxes = document.querySelectorAll(".box");
boxes.forEach(function (box, index) {
box.addEventListener("click", function (evt) {
const el = evt.target;
const day = index + 1;
const arrival = document.querySelector("#arrival" + day);
changeText(el);
setDate(arrival, "2019-12-0" + day);
});
});
But, does the code do what you want to achieve?