You can take only the contents of a div
specific on the return of Ajax.
For example, suppose the page you are on has a div
with id
#preco
with the value of R$ 100,00:
<div id="preco">
R$ 100,00
</div
Now you want to take the new value that returns from Ajax and it is returning all the HTML of the page, but you just want the content of div
#preco
to update to the page you are on. Say you return the HTML below in Ajax:
<html>
<head>
</head>
<body>
<h1>Bla Bla</h1>
<div id="preco">R$ 200,00</div>
</body>
</html>
You want to take the amount that’s on div
#preco
of HTML (R$ 200,00) returned and play on div
#preco
of the current page. For this, you can use the code below in pure Javascript:
var http = false;
http = new XMLHttpRequest();
var url_="pagina.php";
http.open("GET",url_,true);
http.onreadystatechange=function(){
if(http.readyState==4){
var html = http.responseText;
var html = new DOMParser().parseFromString(html, "text/html");
document.getElementById('preco_original').innerHTML = html.getElementById('preco').innerHTML.trim();
}
}
http.send(null);
Or in jQuery:
$.ajax({
url: 'pagina.php',
dataType: 'html',
success: function(html) {
$("#preco").html($(html).filter('#preco').text().trim());
}
});
Can you post your code? Which tool do you use? Use the edit button http://answall.com/posts/136042/edit to add more details.
– Marconi
Itallo, post the font to help us help you :) Tip: In the Ajax request check the datatype you are using. http://www.w3schools.com/jquery/ajax_ajax.asp
– WLoh
You may have a header include where the ajax request is processed, post the source code you are working on for more details...
– Henrique Mendes Silveira Rodri