If you’re asking users to select items from a series of things, you should consider using radio buttons. Using radio buttons will make things easier from a code standpoint by removing the need to set the selected state with javascript, and it will be more accessible for users.
window.onload = function() {
var foods = document.getElementsByClassName('food'); // To get length of all food pictures.
for(var i = 0; i < foods.length; i++) { // for loop to create click function on each image.
var food = foods[i]; // get current class index.
food.onclick = function() { // DOM onclick function.
Array.from(foods).forEach(function(e){e.classList.remove('selected')}) // remove 'selected' class from all 'food' classes.
this.classList.add('selected'); // add selected class to current '.food'.
}
}
}
function select( val ){
for (let i = 0; i < 7; ++i){
document.getElementById(`food${i}`).classList.remove('selected');
}
document.getElementById(`food${val}`).classList.add('selected');
}
// select(1)
I’m creating a meal customisation feature on a website where the customer can choose a variety of different ingredients to configure their meal. I have the back end working perfectly, but I’m having a bit of trouble with the front end.
I’ve used a variety of id’s, classes and functions to create a hover overlay and then apply a border around the food images once they are clicked (selected) but it’s quite a long method. It works, but as there are a lot more ingredients than this (another 30) I’m a bit worried about the functions getting too long.
… + keep book of the currently active element in a variable, it’s easy to refer it when the class needs to be removed.
Comment posted by Laurent S.
You don’t need to store the active one actually… just select the ones that have the class “selected” (technically there should only be 1 at a time) and remove the class, then add it to whichever was clicked.
Comment posted by Andreas
@LaurentS. Yes, but it’s really easy and cheap to
Comment posted by Laurent S.
@Andreas > Yes it’s cheap, but by doing it you still have a risk that the variable is not updated for some edge case. Also regarding your example, you’re not supposed to re-assign a const variable so this would throw an error I think.
Comment posted by Sean
@Zerion this method only lets you select one item at a time.
Comment posted by Andreas
Why
Comment posted by Ahmed Tag Amer
@Sean yes you right, i think this is what he want. If you want to select more than one item at a time tell me.
Comment posted by Ahmed Tag Amer
@Andreas because
Comment posted by Andreas
If you would add a loop you wouldn’t have to re-query the DOM on every click to get the
Comment posted by Andreas
And what’s with
Comment posted by Andreas
You could also reduce the “complexity” of your loop if you remove the
Comment posted by Laurent S.
The least complex would be to not loop on a bunch of elements while knowing only 1 of them is supposed to have that class. Sure you won’t see the difference in most cases, but as your number of elements grows the impact on performance will become more and more visible.
Comment posted by Andreas
Why the
Comment posted by Andreas
There’s a “convention” that only constructor functions that have to be used with the