Reputation: 27
Hello Guys can I just pass the object to another jquery function like the below ?
$('#course_id').on('change', function(e){
var cour_id = e.target.value;
$.get('/ajax-classification?cour_id=' + cour_id, function(data){
console.log(data);
$('#classification_id').empty();
$('#classification_id').append('<option value="">--Select Classification--</option>');
$.each(data, function(index, classificationObj){
$('#classification_id').append('<option value="'+
classificationObj.classification_id+'">'+
classificationObj.classification_name+'</option>');
});
});
//Here i want this function can get back the classificationObj.
$('#classification_id').on('change', function(e){
var cour_id = "1";
var classi_id = e.target.value;
$.get('/ajax-classification-index?classi_id=' + classi_id +'&cour_id='+ cour_id, function(data1){
console.log(data1);
$('#classification_index').empty();
$('#classification_index').append('<option value="">--Select Index--</option>');
$.each(data1, function(index, classificationIndexObj){
$('#classification_index').append('<option value="">'+
classificationIndexObj.classification_index+'</option>');
});
});
});
});
What should I do? How can I modify the first or second Jquery function?
Upvotes: 2
Views: 294
Reputation: 780724
Use .data()
to save the object with each option.
$.each(data, function(index, classificationObj){
$('#classification_id').append($("<option>", {
value: classificationObj.classification_id,
text: classificationObj.classification_name,
data: { object: classificationOBj }
}));
});
Then in the #classification_id
change handler you can use:
var classificationObj = $(this).find("option:selected").data("object");
Upvotes: 1