0

I am using the jquery .get to load the contents of a page. The page loads okay and the ouput is stored in a variable data. I only want to grab a specific div with the id of 'result1' out of that data variable and place it on the page. I am having trouble parsing this specific element from the variable data, it seems that all elements are being placed on the page. I think this is an easy fix but its driving me crazy.

Here is the base code without any parsing attempts:

$.get("test.php?id=" + $(this).attr('id') + "&auth=1" ,function(data){
        $("#details1").html(data);
  });

Here is the results of data:

<div id='result1'>
<table>
<tr>
<td>testa</td>
<td>testb</td>
</tr>
</table>
</div>

<div id='result2'>
<table>
<tr>
<td>testc</td>
<td>testd</td>
</tr>
</table>
</div>
6
  • 1
    You can simply use .load and pass a selector. api.jquery.com/load/#loading-page-fragments Commented Mar 29, 2013 at 19:00
  • Try api.jquery.com/load. See the "Loading Page Fragments" section. Commented Mar 29, 2013 at 19:00
  • I cannot use load given the nature of the code. I have to parse this data variable for specific elements. Commented Mar 29, 2013 at 19:01
  • Yes, you can, because .load() specifically allows you to select parts of the returned HTML. Commented Mar 29, 2013 at 19:02
  • Please explain what about load() you cannot use. Commented Mar 29, 2013 at 19:02

3 Answers 3

1

You can use load()

$("#details1").load("test.php?id=" + $(this).attr('id') + "&auth=1 #result1");

The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.

or you can do the same yourself:

$.get("test.php?id=" + $(this).attr('id') + "&auth=1" ,function(data){
    $("#details1").append( $('<div />').append(data).find('#result1') );
    $("#details2").append( $('<div />').append(data).find('#result2') );
    $("#details3").append( $('<div />').append(data).find('#result3') );
});
Sign up to request clarification or add additional context in comments.

1 Comment

What if I wanted to load result1 into a element details1 and result2 into an element details2 in a single http request?
0

Use this syntax to load only the result1 div in the details1 element.

$('#details1').load("test.php?id=" + $(this).attr('id') + "&auth=1 #result1');

Comments

0
$.get("test.php?id=" + $(this).attr('id') + "&auth=1" ,function(data){
    $("#details1").html($(data).filter('#result1'));
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.