Grow your CSS skills. Land your dream job.

Storing and Saving a Variable Inside an Embedded Function

  • # April 7, 2010 at 11:14 pm

    I’m working with HTML5 databases, and I’m trying to add an auto increment function to a class I’m making. I can get the highest id from my table with SQL and get the next highest id after that, but I can’t save the value.

    I’ve tried to return the new id, but the main function won’t return the value from the function inside of the main function.

    Here’s the function by itself, and it is slightly out of context, but you should be able to understand how it works.

    Code:
    function next_id(table_name, column) {
    if( db ) {
    db.transaction(function(tx) {
    message(‘Getting next ‘ + column + ‘ from table ‘ + table_name + ‘‘);
    tx.executeSql(“SELECT ” + column + ” FROM ” + table_name + ” ORDER BY ” + column + ” DESC LIMIT 1″, [], function(tx, results) {
    highest_id = ++results.rows.item(0).id;
    return highest_id;
    });
    });
    }else {
    message(‘Please initialize a database before inserting a row’);
    }
    }

    The variable "highest_id" has been created at the beginning of the file before the main object/class was created. And I can set the value and run a console.log inside the function after I store it and get the new value, but if I try to run the function var new_id = database.next_id(‘data’, ‘id’); and save the returned value, it doesn’t set an id.

    If you need to see more of the code, or if I haven’t explained something clearly, just let me know and I’ll try to clarify.

    - Baylor Rae’

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".