1

I have div question and one for each answer:

<div id="question"></div>
<div id="a"></div>
<div id="b"></div>
<div id="c"></div>
<div id="d"></div>
<div id="e"></div>

when user click in one div I'd to 'select' it adding a css background.

So, I did:

var divA = document.getElementById('a');
divA.addEventListener('click', function () {
  remove();
  divA.classList.add("select");
},false);

var divB = document.getElementById('b');
divB.addEventListener('click', function () {
  remove();
  divB.classList.add("select");
},false);

... same thing to C, D and E.

I'd like to not repeat the code for each div select. any ideas how to avoid all this divA, divB, divC, divD, divE functions and add all in one?

my remove function:

function remove(){
    document.getElementById('a').classList.remove("select");
    document.getElementById('b').classList.remove("select");
    document.getElementById('c').classList.remove("select");
    document.getElementById('d').classList.remove("select");
    document.getElementById('e').classList.remove("select");
}
4
  • 1
    document.querySelectorAll, select all the elements by a class. Loop over them and add an event handler to them. The event handler can reference the element clicked with the event.target or this within the callback Commented Jan 7, 2020 at 18:50
  • @Taplar humm, no way to use ID instead of class for this? Commented Jan 7, 2020 at 18:53
  • You can use ids, and select multiple ids in a selector, however .sharedclass is much cleaner than #id1, #id2, #id3, #id4, ... Commented Jan 7, 2020 at 18:54
  • @Taplar thank you very much! Commented Jan 7, 2020 at 18:55

1 Answer 1

3

You need to do the following steps:

  • Use a same class for each option. Like I user "answer" in below code.
  • Then use querySelectorAll() to select all the elements in an array.
  • Loop through array and add event listener to each item.
  • In the function passed to event listener. First unselect all the elements.
  • Then select the clicked element.

I have added a little styling from myself.

Note: You can also remove the id from the elements the code will still work.

const answers = document.querySelectorAll('.answer');
answers.forEach(elm => {
  elm.addEventListener('click', e =>{
    //removing all the previous selections
    answers.forEach(x => x.classList.remove('select'))
    //selecting the current one
    e.target.classList.add('select');
  })
})
.answer{
  padding:15px;
  font-size: 30px;
  text-align: center;
  background: lightgray;
  border-bottom:1px solid black;
  font-family: cursive;
}

.select{
  background:  rgb(245, 245, 245)
}

.answer:hover{
  background: rgb(245, 245, 245);
  cursor:pointer;
}
<div id="question"></div>
<div class="answer" id="a">A</div>
<div class="answer" id="b">B</div>
<div class="answer" id="c">C</div>
<div class="answer" id="d">D</div>
<div class="answer" id="e">E</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.