stoerebink stoerebink - 1 year ago 146
Ruby Question

How to add/remove params to link with jquery

I have some select fields that set for example the vendor-id and status for my datatable. When these field change, I want to include these parameters in a link (the link calls a server function that exports the data to csv). My experience with jquery/coffeescript is very limited and I'm having trouble getting this to work. What I have so far is:

$ ->
$('#vendor-id').on 'change', ->
$('').attr 'href', (index, href) ->
param = 'vendor-id='+$('#vendor-id').val();
if href.charAt(href.length - 1) == '?'
href + param
else if href.indexOf('?') > 0
if href.indexOf(param) == 0
href + '&' + param
href + '?' + param

I have two problems with my code. One is that for some reason, when I change the value, the param is added to the link twice (example: )

The second problem is that when I select a different value for this select box, the previous value is not removed from the link but added on to it.

How can I fix this?

Answer Source

You can find some answers here: Change URL parameters. For example: - it worked for me. Just copy updateURLParameter function and use it this way:

$ ->
   $('#vendor-id').on 'change', ->
   $('').attr 'href', (index, href) ->
     updateURLParameter(href, 'vandor-id', $('#vendor-id').val())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download