far1023
far1023

Reputation: 123

Pass argument by jquery change method

Any idea to my problems? I have to pass a value from json data on jquery change. My code look like this now :

$('#id_user').val(data.id_user).change(data.id_level); // need to pass this id_level

And need that data.id_level in :

$('#id_user').change((howToPassThatIdLevelHere), function(){ // I need the id_level in here
   ...
})

Thank you

Upvotes: 0

Views: 39

Answers (1)

Ronnie Tws
Ronnie Tws

Reputation: 484

You can use JSON object to pass data through event.

//Declare of the change event's callback
$('#id_user').change(function(event, eventdata){
    if(typeof eventdata !== 'undefined'){
       var id_level =  eventdata.id_level; //Way to get passing JSON object
    }
});

//Trigger change event with JSON params
$('#id_user').val(data.id_user).trigger("change",{id_level: data.id_level});

Hint: The undefined checking of eventdata is required to avoid error occur, when the change event triggered by user

Upvotes: 1

Related Questions