Using Phonegap with its SQL function, I get my query returns in the form of
results.rows.item(i).Title
where results.rows.item(i) is per object and the .Title will specify which field to select.
This is only one of several fields, so instead of writing it out a massive line several times, I wanted to declare it as a variable.
var details = "results.rows.item(i).Title + \' \' " +
"+ results.rows.item(i).Street + ' ' " +
"+ results.rows.item(i).PostalCode + ' ' " +
"+ results.rows.item(i).Contact + ' ' " +
"+ results.rows.item(i).ContactEmail + ' ' " +
"+ results.rows.item(i).ContactPhone + ' ' " +
"+ results.rows.item(i).OperatingHours + ' ' " +
"+ results.rows.item(i).Operator + '</p></div>'"
Of course the resulting problem is, its a string when I try to use in the following line
$("#locations").append('<div data-role="collapsible"><h3>' + results.rows.item(i).Title + '</h3><p>' + details);
What can I do?
Thanks.
If I’m undestanding your question correctly, then you can use strings as property names by using bracketed notation rather than dot notation:
(Note that that uses ES5’s
forEach. If you may not be running in an ES5-enabled environment, you can use a shim [or rewrite the above to use aforloop].)Basically, in JavaScript, you have two options for accessing the property of an object: You can use a literal property name with a dot, e.g.
foo.bar; or you can use bracketed notation with a string, e.g.foo["bar"]. In the latter case, the string doesn’t have to be a string literal, it can be the result of any expression. So all of these output thebarproperty offoo: