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

99
Visualizações
Select multiple elements and modify using JavaScript

This is my code:

// "cls" returned document.getElementsByClassName()

if (cls('disableCM') != null) {
    cls('disableCM').setAttribute('oncontextmenu', 'return false');
}

I want every tag using class="disableCM" to be returned false. There for i want to set attribute "contextmenu" with "return false". But the upper code only works for the first element.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Getting the basic idea from the question, I assume you are trying to avoid the right click on certain elements. For that to happen you have to attach the event listener to every element. In order to do that below is a piece of working code. The items in grey are disable and if you right click, it will not show the context menu, while on the other elements it will show the context menu.

var disabled = document.getElementsByClassName("disableCM");
Array.prototype.forEach.call(disabled, (element) => {
  element.oncontextmenu = function() {
    return false;
  }
});
.disableCM {
  background: grey;
}

div {
  height: 50px;
}
<div class="disableCM">Element 1</div>
<div class="disableCM">Element 2</div>
<div class="disableCM">Element 3</div>
<div>Element 6</div>
<div class="disableCM">Element 4</div>
<div>Element 5</div>

about 4 years ago · Juan Pablo Isaza 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