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

201
Views
How to change html href attribute value with jQuery?

I am trying to change html href attribute value ?post=55 to ?post=44 with button click using jQuery, but don't know how.

HTML

<button class="button">button</button>

<a href="posts.php?post=55" class="post-link">Post</a>

JAVASCRIPT

$('.button').on('click', function() {

  
});

This is what I have

<a href="posts.php?post=55" class="post-link">Post</a>  

This is what I want when clicking button

<a href="posts.php?post=44" class="post-link">Post</a> 
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

A Jquery function allows you to change an attribute of a tag. It is written as $("tag").attr('attr','newvalue'). In your case, you can do $("a").attr('href','posts.php?post=44') inside the click function (you should probably give the a tag an id too)

about 4 years ago · Juan Pablo Isaza Report

0

"on-click" handler has $(this) that you can use to navigate in the DOM tree if you know that the object you want to alter is relatively placed (from what I see you have <a/> following <button/>.

$('.button').on('click', function() {
    $(this).parent().find('.post-link').attr('href', '#hello-world')
})

JSFiddle

If you want to change a link of the element that is somewhere on the page, I would suggest searching by id value (the id should be unique). Let's say you have <a id="my-id" href="...">...</a>, then:

$('.button').on('click', function() {
    $('#my-id').attr('href', '#hello-world')
})
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!