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

197
Visualizações
Opacity 0 Javascript function

This is my first SO post, please let me know how to do better!

I have a function that clears the by setting the opacity to 0, it works, but it will make my file massive if if try to set up a whole spread sheet with each having the same function bar different ids,

Ideally, the way I want this to play out, is that clears itself, and will clear all blocks. And I want to do it without having to write duplicate functions.

Is it possible to have a function set over classes? I have tried with no success
Or is there a better way to run the JavaScript, like somehow onclick==clear.self ?

function Xf1() {
  f1();
  f2();
}

function f1() {
  var element = document.getElementById("a1");
  element.style.opacity = "0";
}

function f2() {
  var element = document.getElementById("a2");
  element.style.opacity = "0";
<tr>
  <th onclick="Xf1()">Clear all</th>
</tr>

<tr>
  <td onclick="f1()" id="a1"> text1</td>
  <td onclick="f2()" id="a2"> text2</td>
</tr>

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

0

You can use event delegation

  1. start by adding a class to the table element
  2. add a class to the "clear all" heading
  3. add a click event listener to the table element

If the click event target is a td element, set its opacity to 0.

If the click target is the clear all heading, set all td elements to opacity 0. You can do that by querying the table for td tags and then using forEach to change the opacity for each of them.

const myTable = document.body.querySelector(".my-table");

myTable.addEventListener("click", event => {
  const target = event.target;
  if (target.tagName == "TD") {
    target.style.opacity = 0;
  }
  if (target.classList.contains("clear-all")) {
    myTable.querySelectorAll("td").forEach(item => (item.style.opacity = 0));
  }
});
<table class="my-table">
  <thead>
    <th class="clear-all">Clear all</th>
  </thead>
  <tbody>
    <tr>
      <td id="a1">text1</td>
      <td id="a2">text2</td>
    </tr>
  </tbody>
</table>

about 4 years ago · Juan Pablo Isaza Relatório

0

set an id to parent tag then set onclick to that elements children.

if you set "container" as id you will have something like this:

var elements = document.getElementById("container");

for (let i = 0; i < elements.children.length; i++) {
  elements.children[i].onclick = function () {
    elements.children[i].style.opacity = "0";
  };
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of adding onclick to each td element. Have it in table element. Like this

    
    document.querySelector("#table").addEventListener("click", (event)=>{
      if(event.target.dataset.type==='clear'){
        const ids = ['a1', 'a2'];
        ids.forEach((ele)=>{
         document.querySelector(`#${ele}`).style.opacity = '0';
        });
        return;
      }
      event.target.style.opacity = "0";
    }
    )
<table id="table">
  <tr>
    <th data-type="clear">Clear all</th>
  </tr>

  <tr id="tableRows">
    <td id="a1"> text1</td>
    <td id="a2"> text2</td>
  </tr>
</table>

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