Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda