• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

187
Vistas
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 3 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

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 3 years ago · Juan Pablo Isaza Denunciar

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 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda