Question

Can I replace a <div> with external php script. Something like this:

$('#aside').replaceWith('blocks/filename.php');

Please be gentle I have just started to learn JavaScript.

UPDATE:

I want to replace that <div id="aside">. I want to remove it completely and place the new content there.

Was it helpful?

Solution

You can do this - if you want to replace #aside with new content

$.get("blocks/filename.php", function(data) {
    $('#aside').replaceWith($(data));
});

OTHER TIPS

Not that simply, you can load your PHP into said div tho with a simple .load call:

$("#aside").load("blocks/filename.php", function() {
    console.log("I've been loaded!");
})

API Ref: http://api.jquery.com/load/

Per the edits, you'll want to use a $.get function with a callback to replace that div with the new content.

You want to load the contents from a PHP-file, and put it inside a <div> right? The very easy way would be to send a AJAX GET-request to the file, and fill the contents as such:

$.ajax({
  url: 'blocks/filename.php', 
  data: {}, 
  success: function(data) {
    $('#aside').replaceWith(data);
  }), 
  dataType: 'html'
}); 

EDIT: Changed to replaceWith() instead, as suggested.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top