sinθ
sinθ

Reputation: 11493

Replace div using JQuery ajax, not replace contents

Here's an example doc structure

<div>
    <div id="1" class="to-replace"></div>
    <div id="2" class="to-replace"></div>
    <div id="3" class="to-replace"></div>
</div>

What I want to do is replace #1, for example, with whatever I retrieve from the ajax call (I'm not using JSON because the request is extremely small and it didn't seem worth it. Plus, the site is mostly for my personal use). I know I can use JQuery .load() to replace the contents, but there must be some easy way to replace the whole thing.

Upvotes: 0

Views: 541

Answers (2)

user1338871
user1338871

Reputation: 138

$.get(url,function(data){
   $("#1").replaceWith(data);
}); 

where url is the url to whatever you want to get and data is the data that is returned

Upvotes: 3

Eric Beaulieu
Eric Beaulieu

Reputation: 1064

You can use the replaceWith jquery function to replace an element:

    $("#1").replaceWith("bla");

Upvotes: 5

Related Questions