44

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?

1
  • var plz=$response.find('#title').text(); alert(plz); It Throws Null Values ,Should I need to Do any other thing to Get Values Commented Dec 31, 2008 at 5:36

5 Answers 5

87

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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

I am a jQuery novice, but I needed var $response(data.d) to get this to work. added the .d
For me, i tweaked the above to use .html() instead of .text() b/c i needed to retrieve the full html instead of just a text string. i.e. var oneval = $response.filter('#one').html();
if (data.d) { data = data.d }
17

Change the .find to .filter...

3 Comments

Spent 30mins playing with this before I saw your follow up here. Edited to include filter :D
Can you give an explanation why you can't use .find() here? I've tested it and indeed you get result when using .filter() but not when using .find()..
@Yves Van Broekhoven because there is no wrapping root element so the jquery object contains multiple root nodes which you have to filter
12

I have noticed that your success function has the parameter "html", and you are trying to add "data" to your elements html()... Change it so these both match:

$.ajax({
    type:"POST",
    url: "ajax.php",
    data:"id="+id ,
    success: function(data){
        $("#response").html(data);
    }
});

Comments

4

You can use json like the following example.

PHP code:

echo json_encode($array);

$array is array data, and the jQuery code is:

$.get("period/education/ajaxschoollist.php?schoolid="+schoolid, function(responseTxt, statusTxt, xhr){
    var a = JSON.parse(responseTxt);
    $("#hideschoolid").val(a.schoolid);
    $("#section_id").val(a.section_id);
    $("#schoolname").val(a.schoolname);
    $("#country_id").val(a.country_id);
    $("#state_id").val(a.state_id);
}

1 Comment

What about the ending "}"?
2

You may also use the jQuery context parameter. Link to docs

Selector Context

By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $() function

Therefore you could also have:

success: function(data){
    var oneval = $('#one',data).text();
    var subval = $('#sub',data).text();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.