Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!