Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
How to run only one function if we have multiple class

So. to begin with, I am writing my eventlisteners in this way.

document.addEventListener('click',(e)=>{
  const element = e.target;
  if(element.classList.contains('classOne'){
    fire_function_one();
  }
  if(element.classList.contains('classTwo'){
    fire_function_two();
  }
});

I have a div like follows

<div class='classOne classTwo'>Something</div>

So what I want to achieve is, When our div has classOne, I want to fire 'fire_function_one()', However when our div has both classOne and ClassTwo, I want to fire 'fire_function_two()' but I dont want to run 'fire_function_one()'.

What I have tried,

  1. event.stopPropogation; //Not working
  2. event.preventDefault; //Not working
if(element.classList.contains('classTwo' && !element.classList.contains('classOne'){
  fire_function_two();
  //Doesnt acheive what I want
}
about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

Change the Order of your condition and use else if statement.

document.addEventListener('click',(e)=>{
  const element = e.target;
 
  if(element.classList.contains('classTwo'){
    fire_function_two();
  }
  else if(element.classList.contains('classOne'){
    fire_function_one();
  }
});
about 4 years ago · Santiago Gelvez Relatório

0

If you are sure that the element can have classOne or both classTwo and classOne, you can just change the order and use else if statement:

document.addEventListener('click',(e)=>{
  const element = e.target;
  if(element.classList.contains('classTwo'){
    fire_function_two();
  } else if(element.classList.contains('classOne'){
    fire_function_one();
  }
});
about 4 years ago · Santiago Gelvez Relatório

0

You need to write click on element as below.

var eleOne = document.getElementsByClassName('classOne')
if(eleOne.length > 0) {
    var currentEleOne = eleOne[0];
    currentEleOne.onclick = function () {
        // Click code for classOne
    }
}

var eleTwo = document.getElementsByClassName('classTwo')
if(eleTwo.length > 0) {
    var currentEleTwo = eleTwo[0];
    currentEleTwo.onclick = function () {
        // Click code for classTwo
    }
}
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda