Extracting Ajax return data in jQuery
I have done jQuery and Ajax, but I am not able to get the response into a Div element. This is the code:
Index.html
$.ajax({
type:"POST",
url: "ajax.php",
data:"id="+id ,
success: function(html){
$("#response").html(data);
}
});
It is receiving the response to my <div id="response"></div>
.
The ajax.php
returns following code to the index.html
file:
<div id ="one"> OneVal </div>
<div id ="sub"> SubVal </div>
Will I able to extract the OneVal and Subval into a variable, and how can I extract "OneVal" and "SubVal", instead of above response?
Solution 1:
You can use .filter
on a jQuery object that was created from the response:
success: function(data){
//Create jQuery object from the response HTML.
var $response=$(data);
//Query the jQuery object for the values
var oneval = $response.filter('#one').text();
var subval = $response.filter('#sub').text();
}
Solution 2:
Change the .find
to .filter
...