SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to retrieve the value of an ajax dynamically generated element ?

    Hello guys,

    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 ?

    Thank you!

  2. #2
    SitePoint Addict WolfShade's Avatar
    Join Date
    Mar 2014
    Location
    St. Louis, MO, USA
    Posts
    257
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

    V/r,

    ^_^

  3. #3
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 ?

  4. #4
    SitePoint Addict WolfShade's Avatar
    Join Date
    Mar 2014
    Location
    St. Louis, MO, USA
    Posts
    257
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

    ^_^

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    I agree with WolfShade that manipulating the data on the server, then passing it to the JS to display is the better solution.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •