I would use the .find
method in the response content of your request.
It may seem strange at first, this works perfectly.
See:
$.ajax({
url: 'http://agenciaroadie.com.br/loja.php',
type: 'GET',
success: function(res) {
var headline = $(res.responseText).find('#id_da_div_que_eu_quero');
$("#cabecalho").html(headline);
}
});
Note that .text
is not required. This is very useful if you want, for example, to select elements of a specific class.
Example:
$(function(){
// Suponhamos que esse é o conteúdo de outra página
var html = '<ul>\
<li>1</li>\
<li>2</li>\
<p>Não é uma li</p>\
</ul>';
// Quero somente as LI
var $lis = $(html).find('li');
$('#element').html($lis);
})
See this on JSFIDDLE
Example with classes:
$(function(){
var html = '<ul>\
<li class="impar">1</li>\
<li class="par">2</li>\
<li class="impar">3</li>\
<li class="par">4</li>\
</ul>';
// Quero somente os pares
var $lis = $(html).find('li.par');
$('#element').html($lis);
})
See JSFIDDLE
Getting filtered elements from an external content
Suppose we have two pages:
-
index.html
- The page where we are working
-
externo.html
- The external page where we will look for the elements
See how it would work, practically, based on this scenario:
External content:
#externo.html
<div id="conteudo">
<ul>
<li></li>
</ul>
</div>
The page where we will install the contents of another page
#index.html
<!DOCTYPE html>
<html>
<head>
<title>Testando</title>
<script type="text/javascript" src="seu_jquery.js"></script>
<script type="text/javascript">
$(function(){
$.ajax({
url: 'externo.html', // página da requisição externa
type: 'GET',
// parâmetro "html" vem com o conteúdo da página completo
success: function(html) {
// Pegamos somente <li> da página externa
var $lis = $(html).find('#conteudo > ul > li');
// Mandamos para o elemento de id "cabecalho" da nossa página
$("#cabecalho").html($lis);
}
});
});
</script>
</head>
<body>
<div id="cabecalho"></div>
</body>
</html>