I have the following url
http://ejemplo.com/pagina?id=5
and I have an href of the following form
<a href="?estado=Completado">Completado</a>
When the href is clicked, the url remains
http://ejemplo.com/?estado=Completado
But what I want to happen is that when I click on the href, the url stays and the GET is done to the url:
http://ejemplo.com/pagina?id=5&estado=Completado
As I put in a comment, this seems more like a task to be done when the page (and link/button) is generated than with JavaScript once the page has already been served. I'm going to leave you a method to do it with JavaScript
To update the link values, what you could do is read the URL (using
window.location.href
), check if it has a question mark, and add the new parameter appropriately (if there is a question mark it means there are more parameters, so the new one must be added with a&
, while if there are no other parameters you will add it with a?
).Something like this:
You won't see the example working quite right in this snippet because the URL doesn't have any parameters. As a note: it is important that you use
.getAttribute("href")
instead of.href
to read the value ofhref
because otherwise browsers will read the full value of the destination instead of the textual value, and it can cause problems when replacing.I don't know if it will help you but it works if you know pure js leave an alert to guide you
Without further ado, this is the code you need: