On Jun 23, 6:24 am, anawak82 <anawa...@googlemail.com> wrote:
> Hi,
> I am just trying to learn a little bit about jQuery. Now I have a
> problem with returning a value from a function.
>
> The following code works fine, when I tested it with alert(price) or
> any other way of echoing the value directly. But if I try something
> like var test = getPrice(324), the variable test would always be
> undefined.
>
> function getPrice(id)
>        {
>          $.get('db.xml', function(d){
>          $(d).find('data').each(function(){
>                         var $data = $(this);
>                          if ($data.find("id").text() == id)
>                          {
>                                 var price = $data.find("preis").text().trim();
>                                 return price;
>                         }
>          });
>      });
>  };

A semi-colon after a function declaration is an empty statement.  The
getPrice function doesn't have a return statement, so its return value
will always be undefined (provided it executes without error).


> I assume that the nested functions are part of the problem, but I
> don't know how to solve it.

Add a return statement to getPrice, maybe something like:

  function getPrice(id) {
    return $.get('db.xml', function(d){
      ...
    });
  }


--
Rob

Reply via email to