Skip to main content
Inspiring
April 5, 2022
Answered

HTML5 Canvas - Get selected option label for combobox

  • April 5, 2022
  • 1 reply
  • 263 views

How do I get the selected option label for combobox?  

 

function assignDataF(data) {
	for (var i = 0; i < data.length; i++) {
		$('#patients_cbx').append($("<option/>").attr("value", data[i]).text(data[i].label));
	}
}

//Select patient from ComboBox list


$("#dom_overlay_container").on("change", "#patients_cbx", showPatientDetails.bind(this));

function showPatientDetails(evt) {
	root.patientDetailsModal.visibility = true;
	root.patientDetailsModal.patient_name.text = evt.target.innerText;
}

Tried with  evt.target.innerText but it just gives me all the labels in the combobox...

    This topic has been closed for replies.
    Correct answer kglad

    dataA is your jason array of data.

    1 reply

    kglad
    Community Expert
    Community Expert
    April 5, 2022

    use:

     

    function showPatientDetails(evt) {
    	root.patientDetailsModal.visibility = true;
    	root.patientDetailsModal.patient_name.text = labelF(Number(evt.target.value));
    }


    function labelF(value){
    for(var i=0;i<dataA.length;i++){
    if(dataA[i].data == value+1){
    return dataA[i].label;
    }
    }
    }

    FlatChatAuthor
    Inspiring
    April 6, 2022

    @kglad   Thanks.  What is dataA?  I'm getting not defined...

    FlatChatAuthor
    Inspiring
    April 6, 2022

    @kglad 

     

    As I needed to get an array of data in JSON anyway for the 'patient', I went another way, using the evt.target.value of the combobox selected option, then doing another Query AJAX call to the database querying the primary key for that record based on the value of the selected option.  Works fine:

     

    $("#dom_overlay_container").on("change", "#patients_cbx", showPatientDetails.bind(this));
    
    var patient_pk;
    
    function showPatientDetails(evt) {
    	root.patientDetailsModal.visible = true;
    	patient_pk = evt.target.value;
    
    	$.ajax({
    		type: "POST",
    		url: 'scripts/get_patient_details.php',
    		dataType: 'json',
    		data: {
    			patient_pk: patient_pk
    		},
    		success: function (data) {
    			root.patientDetailsModal.patient_name.text = data['patient_name'];
    		}
    	});