0

I using ajax to retrieve a web page like that:

   $.ajax({
            url: "/Cadastros/pagina_busca_correios?cep=" + cep,
            type: "Get",
            DataType: 'Html',
            success: function (data) {                       
                 var webdata = data;
                 var tables = $(webdata).filter('table');
                 alert(tables);                                                
            }
   });

I am able to print the html in data return but after creating the jquery object and apply filter I get noting. It is like a have an error with my javascript.

PS. I know I have 4 tables inside this html string, so I would like to filter the tables and iterate through them to perform actions. Those elements inside the DOM has no id´s or names that I could use to select straight forward.

What I am doing wrong?

4
  • 1
    Are table elements in the root of webdata? Commented Jul 1, 2013 at 21:12
  • 2
    It's hard to say what you're doing wrong without seeing the actual returned html. I suspect you want find instead of filter but I have no way to know. Commented Jul 1, 2013 at 21:12
  • Are your tables wrapped in another tag if so then use find instead of filter Commented Jul 1, 2013 at 21:12
  • Note that you'd normally write the dataType in lowercase and the type in uppercase, not that it would matter as jQuery sorts it out, but using uppercase for the first letter only is bad form, at least a little bit, same goes for assigning new variables that does the exact same thing as the original one. Commented Jul 1, 2013 at 21:15

1 Answer 1

8

filter() doesn't find child elements, find() does. To make it sure it works either way, append the data to a new element and use find() :

var tables = $('<div />').append(data).find('table');

and you can use data directly, no need for another variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.