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

156
Visualizações
Vanilla JS window.location by attribute by classname

I am trying to go from a Bootstrap 4 framework to a Bootstrap 5 framework - which means moving away from jQuery.

I have several elements on a page all with the classname .info-box and an attribute as follows

<div class="card card-icon col-md-4 info-box" data-href="file.php?e=AA">
<div class="card card-icon col-md-4 info-box" data-href="file.php?e=BB">
<div class="card card-icon col-md-4 info-box" data-href="file.php?e=CC">

When the user clicks on the card element, the intent is to open the link. The following JS gets close, but it links all three card elements to the Last Link (=CC)

        var elements = document.getElementsByClassName('info-box');
        for(var i = 0; i < elements.length; i++) {
            var element = elements[i];
            var newLoc= elements[i].getAttribute('data-href');
            element.onclick = function() {window.location = newLoc;}
        }

Can you suggest a way to make the correct data-href attribute link to the correct card?

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

0

I think I found a solution .. Does this seem efficient?

<script>
    var elements = document.getElementsByClassName('info-box');
    [].forEach.call(elements, function(elements) {
      elements.onclick = function() {
        var newLoc= this.getAttribute('data-href');
        window.location = newLoc;
      }
    })
</script>

Found it over here .. javascript: call function by classname

about 4 years ago · Juan Pablo Isaza Relatório

0

var elements = document.getElementsByClassName('info-box');
    for(let i = 0; i < elements.length; i++) {
        let element = elements[i];
        let newLoc= elements[i].getAttribute('data-href');
        element.onclick = function() {window.location = newLoc;}
    }

Use let instead of var.

about 4 years ago · Juan Pablo Isaza Relatório

0

The problem is that var does not have block scope, which means in your case the variable newLocis defined globally. Use let instead of var.

I think I found a solution .. Does this seem efficient?

Your solution should also work fine, because it wraps the var newLoc in a function, which limits its scope. Using let is generally good practise though.

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