Skip to content Skip to sidebar Skip to footer

Turn A Div Into A Button Dynamically

i don't seem to find any way to work this out..so i guess i m going to need some help.. i m making a phonegap application in which i have an sqlite database.I perform a query and g

Solution 1:

You are retreiving always the same 'divid' variable, because of the way javascript scope and closures work. This is a very common mistake. You can google "javascript closure infamous loop" to get more information about this.

You should replace your for with this:

for (var i = 0; i < len; i++) {

    (function () {
        var divid = "#" + results.rows.item(i).id;
        $(divid).on("click", function () {
            alert(divid);
        });
    })();
}

That way, the variable divid is declared in different scopes each time.

I didn't understand if that is the only problem you are having.

Post a Comment for "Turn A Div Into A Button Dynamically"