3

I want to get a div element with an ID after calling $.ajax() Here is my code:

$.ajax({

  url: "PO_Header.php",
  data: "supplier="+selectedId,
  cache: false,
  success: function(html){
    $("#PO_Header").empty(); 

    $("#PO_Header").append(html);

  }
});

$("#PO_Header").append(html); will append the entire html page, what I want to have is get an element with a specific id. Let's say inPO_Header.phpa div element with an id='mydiv' would be injected in my current page.

1

5 Answers 5

5

Use jQuery's load:

$('#PO_Header').load('PO_Header.php #mydiv', { 'supplier': selectedId } );

It allows you to load page fragments. As their documentation points out:

Loading Page Fragments 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.

It will therefore only inject <div id="myDiv"></div> from PO_Header.php into your element.

Sign up to request clarification or add additional context in comments.

3 Comments

so what's the difference between load() and ajax() ? Thank you for providing me with a helpful solution.
$.ajax() is the general function jQuery provides to handle the XmlHttpRequest object. But they added a whole bunch of more precise functions to enable people to get what they want out of it without lots of unnecessary parameters. So $.load() is just a more precise and specified function of $.ajax() which enables you to fetch data from a server file and insert it into a DOM element.
Now all I have to do is use $_POST on my php page because I'm passing a data. Thank you sir.
3

You can use .load() for this.

Loading Page Fragments

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.

Source: http://api.jquery.com/load/

Basically you can use it like this:

$('#PO_Header').load('PO_Header.php #mydiv', { 'supplier': selectedId } );

To disable cache you can use at the start of your code:

$.ajaxSetup({
  cache: false
});

EDIT:

.load() is roughly the same as .get() except for a couple of reasons:

  1. .load() has an implicit callback function which set the returned HTML content into the supplied element when a successful response occurs.

  2. It has a special syntax for the url parameter for specifying just a determined portion of the returned document to be inserted.

  3. Default method is GET. Unless the data parameter is passed as an object then a POST method is used.

2 Comments

so what's the difference between load() and ajax() ? Thank you for providing me with a helpful solution.
folllow up, when should I use ajax over load
2

I prefer to bind to ajax loaded elements in an external function and call that in my success function.

function binder(){
  $("#mydiv")....do something here
}

$.ajax({

  url: "PO_Header.php",
  data: "supplier="+selectedId,
  cache: false,
  success: function(html){
    $("#PO_Header").empty(); 

    $("#PO_Header").append(html);
    binder();

  }
});

3 Comments

The question is how to select the "#mydiv" element from the html returned by the Ajax call, not how to manipulate an existing $("#mydiv") jquery element.
@Simon, That is exactly what this does. The binding function is not called until the ajax success function. Maybe I am not understanding your comment.
I think the original author wants to select the #mydiv element from the ajaxed HTML before it is appended to #PO_Header. At least that's how I understand it.
1

Try this

$.ajax({

  url: "PO_Header.php",
  data: "supplier="+selectedId,
  cache: false,
  success: function(html){

    var new_html = $(html).find('#mydiv').html();

    $("#PO_Header").empty(); 

    $("#PO_Header").append(new_html);

  }
});

2 Comments

I tried it and what it does is it injects the entire html page not just the specified div
I amended the variable names to make it more clear what is going on. I can assure you it works as I use this technique in my own application.
0

I found this example from CSS-Tricks to be very helpful -

http://css-tricks.com/ajax-load-container-contents/

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.