Skip to main content
Inspiring
Apr 5, 2022 7:16 AM UTC
Answered

HTML5 Canvas - Get selected option label for combobox

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 is closed to new replies. Start a new post to keep the conversation going.
    kglad
    kgladCorrect answer
    Community Expert
    Apr 6, 2022 3:19 PM UTC

    dataA is your jason array of data.

    4 replies

    kglad
    Community Expert
    Community Expert
    Apr 5, 2022 3:01 PM UTC

    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;
    }
    }
    }

    Translate
    FlatChatAuthor
    Inspiring
    Apr 6, 2022 12:26 AM UTC

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

    Translate
    FlatChatAuthor
    Inspiring
    Apr 6, 2022 2:34 AM UTC

    @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'];
    		}
    	});

     

    Translate