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

250
Visualizações
how to exchange 2 href with respect to the presence of a text?

I need to exchange two href if a "text" is exact. I tried lots of solutions and I thought I finally found one with a clean way but there is no change and no error. Do you have an explanation ?

let link_1 = document.getElementById('rempl_1').href;
let link_2 = document.getElementById('rempl_2').href;

$(document).ready(function() {
  if (
    document.getElementById('custom_field').innerHTML.indexOf('remplacement') != -1) {
    let tmp = link_1;
    link_1 = link_2;
    link_2 = tmp;
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="row">
  <span class="custom_field" id="custom_field">remplacement</span>
  <a class="rempl" href="my_first_url" title="original" id="rempl_1">First</a>
  <a class="rempl" href="my_second_url" title="remplacement" id="rempl_2">Second</a>
</div>

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

0

You mean this?

Note I removed the need for jQuery since you were not using it anyway

window.addEventListener("load", function() {
  const custom = document.getElementById('custom_field');
  if (custom.innerHTML.includes('remplacement')) {
    const links = document.querySelectorAll(".rempl");
    const href0 = links[0].href;
    const href1 = links[1].href;
    links[0].href = href1;
    links[1].href = href0;
  }
});
<div class="row">
  <span class="custom_field" id="custom_field">remplacement</span>
  <a class="rempl" href="my_first_url" title="original" id="rempl_1">First</a>
  <a class="rempl" href="my_second_url" title="remplacement" id="rempl_2">Second</a>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

string is copied as value when assigned to new variable, so assign a new string to variable will not change href original value.

if change anchor href is what you want, you should add

document.getElementById('rempl_1').href = link_1;
document.getElementById('rempl_2').href = link_2;

for this scenario, i prefer store element as variable:

let a1 = document.getElementById('rempl_1');
let a2 = document.getElementById('rempl_2');

$(document).ready(function () {
  if (
    document.getElementById('custom_field').innerHTML.includes('remplacement')
  ) {
    [a1.href, a2.href] = [a2.href, a1.href];
  }
});

PS: maybe you want write replacement instead of remplacement

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