I have stored some names in h3 tags, and when I press a button, I want them to be highlighted just one at a time, and stop at one random name.
<div class="all-names">
<h3 class="name-one"><span class="line">Name One</span></h3>
<h3 class="name-two">Name2</h3>
<h3 class="name-three">Name3</h3>
<h3 class="name-four">Name4</h3>
<h3 class="name-five">Name5</h3>
<h3 class="name-six">Name6</h3>
<h3 class="name-seven">Name7</h3>
<h3 class="name-eight">Name8</h3>
<h3 class="name-nine">Name9 <span class="arrow"></span></h3>
</div>
Name one must be crossed off if it is not a Monday, and it will not be highlighted either if it’s not a Monday.
const nameOne = document.querySelector(".name-one")
const goBtn = document.querySelector(".btn")
let allNames = document.querySelector(".all-names")
const isOne = document.querySelector(".answer")
const date=new Date();
const isMonday = date.getDay();
if(isMonday === 1) {
setTimeout(() => {
isOne.textContent = 'yes'
}, 600);
} else {
setTimeout(() => {
isOne.textContent = 'no'
}, 600);
let nameArray = []
goBtn.onclick = function() {
const elements = document.getElementsByTagName("h3")
for (i=0; i < elements.length; i++) {
teamArray.push(elements[i])
// let noNameOne = nameArray.shift() //i would like to remove nameOne whenever it is not a monday;
let randomName = Math.floor(Math.random() * nameArray.length)
nameArray[randomName].style.backgroundColor= "white"
nameArray.forEach(randomName => {
nameArray[randomName].style.backgroundColor= "none"
});
}
}
The code as it is now, it will generate console error:
TypeError: Cannot read properties of undefined (reading 'style')
at app.js:44:35
at Array.forEach (<anonymous>)
at goBtn.onclick (app.js:43:19)
(
If i remove the forEach bit, when I press the button it will highlight several random names. So the problems are these :
- On button press, I should get a single h3 element be highlighted, then the highlight removed and the next element should be highlighted. I should add that every element can be highlighted multiple times, not just once;
-> this should be a rather speedy iteration, until it randomly stops at one element that will stay highlighted.
Then, I should be able to press the button again and the whole process to run again.
3
Answers
In this first part:
randomName
is a number, the index of a random element in the arraynameArray
.In this second part:
randomName
is an element from the arraynameArray
. Please see documentation offorEach
for details.So there is no need to look up the element in the array
nameArray
, you already have that element inrandomName
. Just use it directly:That should fix the TypeError you got.
It however does not implement the desired functionality.
In addition to the answer by @KompjoeFriek, I took the liberty to modify your code a bit to achieve what I believe to be the expected functionality, with some comments added in.
The way I’d approach it is as follows, with explanatory comments in the code:
JS Fiddle demo.
References:
data-*
attributes.Array.prototype.filter()
.Array.prototype.forEach()
.Array.prototype.includes()
.document.createElement()
.document.querySelector()
.document.querySelectorAll()
.Element.classList
API.Element.prepend()
.Element.remove()
.Element.querySelector()
.Element.querySelectorAll()
.HTMLElement.dataset
.Math.floor()
.Math.random()
.