• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Replacing Element Body with jQuery.load(), What Happens To Old Elements?

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 PART QUESTIONS

(1) This is probably a simple question but I've been unable to find a conclusive answer via the search gods. I'm using jQuery's load() function to replace the body of an element with some new HTML, pretty standard stuff. However, if the element in question already contains HTML will the existing elements that are being replaced be removed from the DOM? For example, say I have the following:



And then doing a $('#replace_me').load(....), will the P element above be removed from the DOM? What would be a good way for me to test that this is the case?

(2) Let's assume the answer to part 1 is YES, the elements to be replaced are removed from the DOM. What about JavaScript that might have come down with the new response? What happens to variables and the like that are eval'd from the load() function when the new response comes in. So assume something like:



What happens to the name variable when the contents of 'replace_me' gets replaced?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65126
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(1) Poof!

(2) The script will be loaded but existing declarations will not be removed. Think of it as cumulative.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:(1) Poof!


Great! That's what I thought.

Bear Bibeault wrote:(2) The script will be loaded but existing declarations will not be removed. Think of it as cumulative.


Ok, this could possibly be the root of my problems then. There's a small chunk of JavaScript that has to come down each time the new response to returned. The thing is, the HTML is the same each time, the data is just different. But the HTML needs to be rendered with some jQuery-UI love, so it was easier for me to return the HTML/JavaScript vs just the data and trying to plug it all in where it needed to go. Would using the jQuery.live feature help me out here maybe? So this is basically the JavaScript that comes down with each request:



Some of this will be cleaned up later. ;) As you can see, I'm using a few plugins to get my work done. And each time the new HTML is returned, this stuff needs to happen.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65126
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.live() will help you establish event handlers on a pro-active basis.

The livequery plugin also gives you a notification when a matching element comes into (or out of) being.

Very useful combination for Ajax environments.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic