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

153
Views
Using Jquery to append to all href and src attributes

I need to append the name of my github repository to the beginning of all href and src attributes. Any help is appreciated.

<script>
  $(document).ready(function() {
    $(src).attr('src', '/Demo_App').each();
    $(href).attr('href', '/Demo_App').each();
});
</script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

  1. Select all nodes on the DOM with
document.getElementsByTagName('*')
  1. Loop through them. If has src, update the src with repo name in front. If href, do the same thing but with the href instead.

const repoName = '/Demo_App';

window.addEventListener('load', () => {
    const all = document.getElementsByTagName('*');

    for (const node of all) {
        const src = node.getAttribute('src');
        const href = node.getAttribute('href');

        if (src) node.setAttribute('src', `${repoName}${src}`);
        if (href) node.setAttribute('href', `${repoName}${href}`);
    }
});
<body>
    <a href="https://google.com"></a>
    <img src="https://google.com" />
    <a href="https://google.com"></a>
    <img src="https://google.com" />
    <a href="https://google.com"></a>
    <img src="https://google.com" />
    <a href="https://google.com"></a>
    <img src="https://google.com" />
</body>

about 4 years ago · Juan Pablo Isaza Report

0

use jQuery's attr() with a callback function to append values.

For example

const suffix = "/Demo_App";

// all elements with `src` attribute
$("[src]").attr("src", (_, src) => src + suffix)

// all elements with `href` attribute
$("[href]").attr("href", (_, href) => href + suffix)

As always you might not need jQuery

const suffix = "/Demo_App";

document.querySelectorAll("[src]").forEach(elt => {
  elt.src += suffix
})

document.querySelectorAll("[href]").forEach(elt => {
  elt.href += suffix
})
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!