MarwaInsat
MarwaInsat

Reputation: 293

can't get the results of select query in an array with javascript

I have a problem when trying to store the results of a select query into an array with java script . the problem is that inside the function the array is containing the right values but outside it's empty , even when i used a global var it's still empty !!!

db.transaction(function (transaction) {
    var sql = "SELECT * FROM Question where idEnq=" + idEn;

    transaction.executeSql(sql, undefined, function (transaction, result) {
        var res = document.getElementById('results');
        res.innerHTML = "<ul>";
        if (result.rows.length) {
            for (var i = 0; i < result.rows.length; i++) {
                var row = result.rows.item(i);
                ch[i] = new qu(row.id, row.text, row.type);
                res.innerHTML += '<li>' + row.id + '&nbsp;' + ch[i].text + '&nbsp;' + ch[i].type + '</li>';
            }
            tablo = ch;
        } else {
            alert("No choices");
            res.innerHTML += "<li> No choices </li>";
        }

        res.innerHTML += "</ul>";
    }, onError);
}); // here the ch and the tablo array are empty

Upvotes: 1

Views: 274

Answers (2)

Amira Manai
Amira Manai

Reputation: 2599

you're new so have look here http://pietschsoft.com/post/2008/02/JavaScript-Function-Tips-and-Tricks.aspx there's a part talking about calling JavaScript Function asynchronously

Upvotes: 0

ThiefMaster
ThiefMaster

Reputation: 318508

You are using asynchronous functions. Anything that wants to use the data "returned" by those functions need to be in the callback. Of course you can assign this data e.g. to a global variable, but that variable will only have the value after the callback has run (asynchronously).

Upvotes: 4

Related Questions