szerz
szerz

Reputation: 257

How to insert data in function parameters in javascript?

  1. I initialize Datatable on the page:

    $("#datatable-buttons").DataTable()
    

    The problem is that I need to form a parameters (300 lines of code) for this Datatable, so I make an object:

  2. var myfunctions = {
    
    parametrs: {
                ajax: {
                    type: "POST",
                    data: data, 
                    .....
            }, 
    
    inittable:  function() {
          a = $("#datatable-buttons").DataTable(this.parametrs);
    } 
    

    The problem is that data has to change depending on user input. So I make a function:

  3.  var requestdata =  function() {
        return document.getElementById("daterange").value; 
     };
    
  4. I change a data in parameters to this function

    ajax: {
        type: "POST",
        data: requestdata()...
    

    However, when page is first time initialized the function is being called and everything work excellent however when the code calls this function after user's input, it uses the parameters from initial page initialization, not asking it current (changed!!!) value.

Upvotes: 2

Views: 814

Answers (1)

Vigneswaran Marimuthu
Vigneswaran Marimuthu

Reputation: 2532

It is using initial data because when myfunctions object is created, the parameters object is created with initial data.

var requestdata =  function() {
    return document.getElementById("daterange").value; 
};

var myfunctions = {

    parametrs: {
            ajax: {
                type: "POST",
                data: requestdata(),   // Pass this data as a function parameter
                .....
        }, 

    inittable:  function() {
        a = $("#datatable-buttons").DataTable(this.parametrs);
    }
}

Instead of saving the data, pass the data to the function call.

Upvotes: 2

Related Questions