Search code examples
javascriptjqueryjsonwebformsweb-frontend

How to use the changed value of dropdown in JSON object?


I'm having a problem, maybe it would be easy one but for me it has become a headache.

I've a dropdown menu. There it is:

<select id="locationSelector" name="locationSelector">
   <option value=0 >All</option>
   <option value=1>Lahore</option>
   <option value=2>Karachi</option>
</select>

I get its changed value as follows and want to use its value outside the on-callback in a JSON object as given:

var loc = 0;
$("#locationSelector").on('change', function(e, loc){
        loc = $("#locationSelector").val();
});

TablesDatatables.condition = {'role.role_id': '2|3', 'location.id': loc };

NOTE: I've used loc variable in the above object.

But it's not working. Can you please help me in this regard?


Solution

  • I believe because "location" is in your callback it is being referenced as a local variable. Try changing that callback variable name, I'm not sure what it's used for anyway.

    var location = 0;
    $("#locationSelector").on('change', function(e, locationCHANGED){
        location = $("#locationSelector").val();
    });
    
    TablesDatatables.condition = {'role.role_id': '2|3', 'location.id': location   };