Thomas Williams
Thomas Williams

Reputation: 1548

Jquery load : change how many variables to send

I know that there are lots of threads on this site for sending variables to the jquery load function, but I have a question I can't seem to find an answer to.

Take the following code

$( "#table_form" ).load(url, {
                        STARTDATE: $(startDate).val(),
                        ENDDATE: $(endDate).val(),
                        USER_ID: something
                    },function()
                {
                    // do something after
                });

Now this works. It loads the url and sends three variables fine, using post.

Now say I want to run this load, but this time with only 2 variables eg STARTDATE and ENDDATE.

So I thought I would put the send variables in a string and pass that. That way I could send a dynamic amount of variables

var mystuff="STARTDATE: $(startDate).val(),ENDDATE: $(endDate).val()";

 $( "#table_form" ).load(url, {
                        mystuff
                    },function()
                {
                    // do something after
                });

I thought if I put all my stuff in a string then I could send as many or few variables as I required. However when I put them in a string it gave me an error.

So is there a better way of doing this, or another method of sending a variable amount of variables into jquery load?

Upvotes: 0

Views: 34

Answers (2)

Pankaj Joshi
Pankaj Joshi

Reputation: 14

The second argument to load is an object and you must pass it as key:value pair. Try this:

    var mystuff="STARTDATE: $(startDate).val(),ENDDATE: $(endDate).val()";

 $( "#table_form" ).load(url, {
                      mystuff:mystuff
                    },function()
                {
                    // do something after
                });

Upvotes: -1

Satpal
Satpal

Reputation: 133423

Create a JS object or JSON instead of string.

var mystuff = {
    STARTDATE: $(startDate).val(),
    ENDDATE: $(endDate).val()
};
$("#table_form").load(url, mystuff, function() {
    // do something after
});

Upvotes: 2

Related Questions