1

I am trying to use a nested ajax call and its not working. In the outer .get call, I am pulling in ISBN data from the php file. This will be used to populate the html table named "booklist". This one works fine. The inner .get call is supposed to validate the ISBN and if it returns valid to then append a checkmark to it, but this last part doesn't work. In this code, I was assuming that the variables "index" can be seen inside the inner .get. Is this not true?

    $(document).ready(function()
    {   
        $("input#autofill").click(function()
        {
            $.get("last_year_ISBN.php",
                      function(data)
                      {
                          var elements = $('#booklist .ISBN_number');
                          $.each(data, 
                                function(index, obj)
                                {
                                    $(elements[index]).val(obj[16]);
                                    $.get("validate_isbn.php", 
                                        function(answer) 
                                        {
                                            if (answer == "valid")
                                            {
                                                $(elements[index]).append(" < img src = 'pics/green_checkmark.png' > ");
                                            }
                                        },
                                        "text");
                                })

                      },
                      "json");                                              


        });

        $("input#clearISBNs").click(function()
        {
            var elements=$('#booklist .ISBN_number');
            $.each(elements, 
                  function(index, value)
                  {
                    $(elements[index]).val("");
                  });
        });
    });
3
  • it should be visible... anything in the outer scope shoudl be visible within that func. Im wondering if maybe its the spaces between < and img that is the issue - have you tried adjusting that markup or are you sure that markup like this has worked elsewhere? Nov 2, 2011 at 22:46
  • No, that doesn't make a difference. And anyways, that code should have shown up in the source code once I clicked on the button, but it didn't... Nov 3, 2011 at 1:11
  • for some reason, its not going into the function(answer) (when tested in firebug... why is that? Nov 3, 2011 at 1:36

1 Answer 1

0

It shouldnt make a diff but try creating the function as a variable outside the get call...

function(index, obj){
  var ele = $(elements[index]).val(obj[16]);
  var func = function(answer){
    if (answer == "valid") {
      ele.append('<img src="pics/green_checkmark.png" />');
    }
  };
  $.get("validate_isbn.php", func, "text");
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.