Reputation: 145
I have a question that has me stumped in regarding to jquery and dynamic tables.
I have a page that has multiple elements on it. We are trying to come up with a way to take multiple elements and either add or subtract number to come up with new elements that are not currently on the page. To do this for all kinds of customers that don't all want the same elements added on the page. So we can up with table in a database that has all our elements in a table in the form of jquery commands. For example
$('.field1').val() - $('.field2').val()
The above is stored as a variable that we are going to retrieve from the database. If I were to actually write this in the console window, I would get a value. Our issue is that we want to have these to values subtracted from one another (evaluated) and have a value stored in another variable. I guess something equivalent to the eval function in Javascript. I am not sure if such a thing exists.
To clarify this some more check out the example below.
var example1 = "$('.field1').val() - $('.field2').val()"
The above variable "example1" already holds the string that we want to execute. Is there an easy way to execute this code.
Any help would be appreciated.
Upvotes: 0
Views: 93
Reputation: 2368
EDIT:
Lose the quotation marks, by adding them it's referencing your variable as a string, example:
var example1 = $('.field1').val() - $('.field2').val();
to execute, you can just call example1:
example1;
Or if you prefer it sits alone in a function:
function example1() {
$('.field1').val() - $('.field2').val();
}
Then to call as a function:
example1();
Upvotes: 1