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

158
Views
How to change the href inside of a div using javascript?

I would like to change the href of the in this code:

<div class ="amp-logo">
    <a href="http://www.google.com" title="Google"></a>
</div>

I tried to use this code:

<script>
let link = document.querySelector('.amp-logo').getElementsByTagName('a');
link.href = window.origin+'/blog';
<\script>

but my code is not working. Someone can help me?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem with your code is that getElementsByTagName returns an array of objects. You could make it work like so:

let link = document.querySelector('.amp-logo').getElementsByTagName('a');
link[0].href = window.origin+'/blog'; // see the [0] here
<div class="amp-logo">
    <a href="http://www.google.com" title="Google">X</a>
</div>

But you can select the <a> element directly like so

let link = document.querySelector('.amp-logo > a');
link.href = window.origin+'/blog';
<div class="amp-logo">
    <a href="http://www.google.com" title="Google">X</a>
</div>

or you could also only use the title attribute selector like so

let link = document.querySelector('[title="Google"]');
link.href = window.origin+'/blog';
<div class="amp-logo">
    <a href="http://www.google.com" title="Google">X</a>
</div>

or a combination of both methods like

let link = document.querySelector('.amp-logo [title="Google"]');
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!