JQuery and link generation

How can I get jQuery to create a link to get one so that I don’t have to try to pass all the query parameters?

Alternative to this:

$('a.csvBtn').attr('href',"CSVReporter?cam=" + id + "&range);

As you know jquery.get({url:n, data:xxx});, jQuery takes data and builds a link to the url http://blah.com/n?x=1&x=2 .

+3
source share
1 answer

You can use jQuery.paramto convert the object to a query string.

var qs = {};
qs.cam = id; 
qs.range = 'blah';
var query_string  = jQuery.param(qs); //for id=foo, this will be cam=foo&range=blah

Or, in use :

$('a.csvBtn').attr('href', function(){
     var qs = {cam : id, range : 'blah'};
     return "CSVReporter?"+jQuery.param(qs); 
});
+4
source

All Articles