Javascript - Performing a function after first clicking on an item and then clicking on another item

Asked

Viewed 82 times

1

I have two lists and wanted to make that by clicking first on the first list and then on the second, perform a function to make the two items green.

<p class="question"><span class="mark-question">1.</span> Relacione as fabricantes aos seus veículos</p>
<ul class="first-column">
    <li data-answer="0,5">Ford</li>
    <li data-answer="2">Fiat</li>
    <li data-answer="3">Renault</li>
    <li data-answer="">Ferrari</li>
    <li data-answer="4" id="honda">Honda</li>
    <li data-answer="1">Chevrolet</li>
</ul>

<ul class="second-column">
    <li>Edge</li>
    <li>Ônix</li>
    <li>Palio</li>
    <li>Sandero</li>
    <li id="civic">Civic</li>
    <li>Fusion</li>
    <li>Fusquinha</li>
</ul>

1 answer

1


Creates a variable clicked (for example) to store the already clicked element. Then you can make a simple color logic when you click and the variable already has an element there.

let clicked = null;
const first = document.querySelector('.first-column');
const second = document.querySelector('.second-column');

first.addEventListener('click', clickChecker);
second.addEventListener('click', clickChecker);

function clickChecker(e) {
  const el = e.target;
  if (!clicked) return clicked = el;
  else style(clicked, el);
  clicked = null;
}

function style(a, b) {
  a.style.backgroundColor = b.style.backgroundColor = '#ccf';
}
<p class="question"><span class="mark-question">1.</span> Relacione as fabricantes aos seus veículos</p>
<ul class="first-column">
  <li data-answer="0,5">Ford</li>
  <li data-answer="2">Fiat</li>
  <li data-answer="3">Renault</li>
  <li data-answer="">Ferrari</li>
  <li data-answer="4" id="honda">Honda</li>
  <li data-answer="1">Chevrolet</li>
</ul>

<ul class="second-column">
  <li>Edge</li>
  <li>Ônix</li>
  <li>Palio</li>
  <li>Sandero</li>
  <li id="civic">Civic</li>
  <li>Fusion</li>
  <li>Fusquinha</li>
</ul>

  • Poxa valeu mano! But, how could I use the date to define when I should perform the function of changing the color or not? For example, I wanted you to change the color only when I first click on the "Honda" and then on the "Civic".

  • @Pedrolucasflor in that case you can join if (!clicked && !this.classList.contains('first-column')) return clicked = null; before the first if in function.

  • Thank you very much! But, one last question I copied your code to see how it worked and Chrome is telling me this message "Uncaught Typeerror: Cannot read Property 'addeventlistener' of null at main.js:5". I have had this problem with addeventlistener. I would like to know why?

  • @Pedrolucasflor that means that the document.querySelector did not find the elements on the page and therefore one of the variables first or second has no value.

  • I managed to solve it! I was calling the script at head. This meant that "first-column" and "Second-column" did not exist

  • @Pedrolucasflor boa!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.