Sam Becker
Sam Becker

Reputation: 19646

How would I use jQuery to grab the contents of a page and render it within a div?

This is a question in response to thise: Javascript AJAX function not working in IE?

I need jQuery to do something like this:

function render_message(id)
{
var xmlHttp;
  xmlHttp=new XMLHttpRequest();  
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
        document.getElementById('message').innerHTML=xmlHttp.responseText;
        document.getElementById('message').style.display='';
        }
    }
    var url="include/javascript/message.php";
    url=url+"?q="+id;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

Can someone write the function for me quickly?

Upvotes: 0

Views: 153

Answers (3)

vezult
vezult

Reputation: 5243

Use JQuery's $.load() function (http://docs.jquery.com/Ajax/load):

$("#mydiv").load("a/url/here.html");

Upvotes: 0

Paolo Bergantino
Paolo Bergantino

Reputation: 488644

You can use the handy load() function for this:

$('#message').load("include/javascript/message.php", {q: id}, function() {
    $(this).show();
});

The callback function is assuming the message div is hidden and you only want it show once the request is complete.

Upvotes: 5

Jason Cohen
Jason Cohen

Reputation: 83061

See $.ajax() to retrieve pages and access the content. Documentation here.

Then use e.g. $("#yourElementId").html( myHtmlContent ) to replace the HTML. More doc here.

Upvotes: 1

Related Questions