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?
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?
Share Improve this question asked Jul 1, 2013 at 21:08 Guilherme LongoGuilherme Longo 6553 gold badges9 silver badges20 bronze badges 4-
1
Are
table
elements in the root of webdata? – Claudio Redi 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 offilter
but I have no way to know. – James Montagne Commented Jul 1, 2013 at 21:12 - Are your tables wrapped in another tag if so then use find instead of filter – PSL 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. – adeneo Commented Jul 1, 2013 at 21:15
1 Answer
Reset to default 8filter()
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.