Wednesday 31 May 2017

Cannot retrieve selected values/text from dynamically created using javascript

I am using NodeJS to make CRUD transactions with MySQL.

And I have created a Dynamic Combo box that load all the makes of a car:

input1 = document.createElement('select')
input1.id = 'companyCmb'
let conn = new mysql.createConnection({
    host : 'localhost', database : 'testdb',
    user : 'root', password: ''
})

conn.query('select make_id, make from make_mst',(err,res) =>{
    if(err){
        alert('Error Fetching Company Data')
    }else{
        for(var i=0; i < res.length ; i++){
            var id = res[i].make_id
            var make = res[i].make
            var option = document.createElement('option')
            option.textContent = make;
            option.value = id;
            input1.appendChild(option)
        }
    }
})

This combo is loaded with values just fine.

However when I want to retrieve the selected value on this combo using following code:

var companyCmb = document.getElementById('companyCmb');
var co = companyCmb.options[companyCmb.selectedIndex].value;

It returns values as -- Select -- But that isn't any option given there as well.

Please help

Thank you



via zedkazi

No comments:

Post a Comment