The following code was taken from http://ofps.oreilly.com/titles/9781449383268/ch05.html, in which one learns to use JS with a database.
What I’d like to know is how to add some text along with the variable being inserted into the database. For instance, the user may type CARROTS in a “food” field. So CARROTS would go into the food field of the database. What I’d like to do is prepend FOOD: to the term the user types in so they will be paired together in the database, and later seen together when pulled from the db: FOOD: CARROTS
This is the code where the user’s input is added to the db. How do I modify it to prepend a text to the user’s input? I’m not at all proficient at JS. This is the SQLite database.
Thanks!
function createEntry() {
var date = sessionStorage.currentDate;
var calories = $('#calories').val();
var food = $('#food').val();
db.transaction(
function(transaction) {
transaction.executeSql(
'INSERT INTO entries (date, calories, food) VALUES (?, ?, ?);',
[date, calories, food],
function(){
refreshEntries();
jQT.goBack();
},
errorHandler
);
}
);
return false;
}
... some code here ...
function(transaction) {
transaction.executeSql(
'SELECT * FROM entries WHERE date = ? ORDER BY food;',
[currentDate],
function (transaction, result) {
for (var i=0; i < result.rows.length; i++) {
var row = result.rows.item(i);
var newEntryRow = $('#entryTemplate').clone();
newEntryRow.removeAttr('id');
newEntryRow.removeAttr('style');
newEntryRow.data('entryId', row.id);
newEntryRow.appendTo('#date ul');
newEntryRow.find('.label').text(row.food);
newEntryRow.find('.calories').text(row.calories);
newEntryRow.find('.delete').click(function(){
var clickedEntry = $(this).parent();
var clickedEntryId = clickedEntry.data('entryId');
deleteEntryById(clickedEntryId);
clickedEntry.slideUp();
});