Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda