I am populating a select element dynamically through an ajax call that returns a JSON object with several attributes like so:
$.ajax({
type: "GET",
url: "get_data.php",
data: {value:someValue},
dataType: "json",
cache: false,
success: function(dataSet){
$("#mySelect").empty().append('<option value="">default option</option>');
if (!$.isEmptyObject(dataSet)){
$.each(dataSet, function(i, data){
var option = $("<option>");
option.val(data.id);
option.text(data.description);
option.data("test", 1);
$("#mySelect").append(option);
});
}
$("#mySelect").removeAttr("disabled");
},
error: function(data){
alert("Populate mySelect Error: " + data.error);
}
});
The bit about option.data("test", 1) was me playing around with the idea of adding additionally returned attributes to my dynamically created option elements. This doesn't seem to be working. Any insight on how I can get my additional data attached to my option elements? I'm pretty new to jQuery and client side scripting, so any help is greatly appreciated!
datain jQuery will not be visible in the HTML source, only by accessing the object through jQuery itself.