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

119
Visualizações
Replace in variable a href address

I am beginner web developer. I have small problem with my code:

var nodes = document.querySelectorAll('.breadcrumb-item');

    if(nodes != undefined && nodes !=''){
      var first = nodes[0];
      console.log('xxxxxx' , first);
    }

It's return me:

[Log] xxxxxx
<li role="presentation" class="breadcrumb-item">
<a href="#/" class="router-link-active" target="_self"> Home </a>
</li>

I need check:

  1. if a href is "Home" then replace href from "#/" to "/dashboard"
  2. if a href is not "Home" then show console.log('not find').

How can I make it?

I have dynamic string. For example:

<li role="presentation" class="breadcrumb-item">
<a href="#/" class="router-link-active" target="_self"> Home </a>
</li>

<li role="presentation" class="breadcrumb-item">
<a href="#/dogs" class="router-link-active" target="_self"> Dogs </a>
</li>

<li role="presentation" class="breadcrumb-item">
<a href="#/calls" class="router-link-" target="_self"> calls </a>
</li>

<li role="presentation" class="breadcrumb-item">
<a href="#/catds" class="router-link-" target="_self"> cats </a>
</li>

I need replace URL ONLY for Home

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

0

I've written assuming that <a href="#/">Home </a> will always be first in order. If you're going to change its order, you'll will have to change the index for nodes inside if block.

const nodes = document.querySelectorAll(".breadcrumb-item");

if (nodes.length > 0) {
  const first = nodes[0].firstElementChild;

    first.href = "/dashboard";
    console.log(first);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Previous answer is correct. Maybe the following gives you a clearer understanding:

    const nodes = document.querySelectorAll('.breadcrumb-item');

    if (nodes.length > 0) {
      const first = nodes[0].firstElementChild;
      let currentUrlTitle = first.text.trim().toLowerCase();
  
      if (currentUrlTitle == 'home') {
        console.log(first.text);
        first.href = '/dashboard';
        console.log(first.href);
      } else {
        console.log(currentUrlTitle);
      }
    }
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