I’m stuck, I’m generating a pricelist with an ajax query, and I d’like to use a sum jquery function to display the total price of every line.
But I can’t, because my function doesn’t “see” the dynamically created elements and their values.
Can you help me ?
I’m assuming you’re using some kind of server-side solution to get the prices. Would it be possible to sum the amount as the server is looping through the values and pass that along with the prices?
You could assign a class to each element that contains a price, strip out any dollar signs, commas, etc., and calculate the total after it’s all loaded, but that seems like more work, to me.
The price are calculated in multiple external functions linked to a database, it would be really hard for me to sum them in a php global variable.
All the line prices are echoed in TD elements that have the same .price class…but I guess there are invisible to jQuery because of the ajax load.
Really no solution ?
If the ajax is happening after the page is loaded, no, those TD elements won’t be “seen” by jQuery. You’d have to trigger a function as a callback after the ajax is done.
What I was suggesting as far as the sum and server-side solution was to declare a variable in the script that gets the data from the database and set it to zero, then as each price goes through whatever functions, once that price is set you can add that to the variable, then include the total of that variable in the results that ajax gets. This will allow you to return a decimal value and format it as it’s displayed.
Just a thought.
I agree with WolfShade that manipulating the data on the server, then passing it to the JS to display is the better solution.