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

207
Visualizações
Change div border-color by clicking on div

I have a question regarding this given code: https://stackoverflow.com/a/34404172/11460885.

My situation is, I have different divs and some of them should have the border-color red, when clicking on them and some of them should be green.

How could I change the JS, so I can have more than on color, depending on the parameter?

JS:

function fnChangeBorder(boxId)

{document.getElementById(boxId).style.border = "solid #AA00FF";}

HTML:

<div class="box" id="A" onclick="fnChangeBorder('A')">Click here !!</div>

Thank you all very much.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <style>
      .green-border {
        border: green 0px solid;
      }
      .red-border {
        border: red 0px solid;
      }
      .show-border {
        border-width: 1px;
      }
    </style>
  </head>
  <body>
    <div class="green-border">Im green</div>
    <div class="red-border">Im red</div>
    <div class="green-border">Im green</div>
    <div class="red-border">Im red</div>
  </body>
  <script>
    document.querySelectorAll(".green-border,.red-border").forEach((div) => {
      div.addEventListener("click", () => {
        div.classList.contains("show-border")
          ? div.classList.remove("show-border")
          : div.classList.add("show-border");
      });
    });
  </script>
</html>

about 4 years ago · Santiago Trujillo Relatório

0

You can create a second parameter in your function to do that

/*----- Defining Function ------*/
        function changeBorder(elementID, colorHex){

            /*----- Getting Element & Assign new borders ------*/
                document.getElementById(elementID).style.border = "solid "+colorHex;

        }
<!-- Turns Red -->
<div class="box" id="A" onclick="changeBorder('A', '#ff0000')">I'll be red</div>

<!-- Turns Green -->
<div class="box" id="B" onclick="changeBorder('B', '#00ff00')">I'll be green</div>

about 4 years ago · Santiago Trujillo Relatório

0

function fnChangeBorder(boxId)
{
    var color = "";
    if(boxId === 'A')
    {color = "solid #AA00FF";}
    else if(boxId == 'B')
    {color = "solid black";}
    document.getElementById(boxId).style.border = color;
}

This is really simple js, perhaps you should check out https://www.w3schools.com/js/js_if_else.asp

about 4 years ago · Santiago Trujillo 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