wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes Replacing Element Body with jQuery.load(), What Happens To Old Elements? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Replacing Element Body with jQuery.load(), What Happens To Old Elements?" Watch "Replacing Element Body with jQuery.load(), What Happens To Old Elements?" New topic
Author

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

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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


GenRocket - Experts at Building Test Data
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

(1) Poof!

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

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Jan 10, 2002
Posts: 61424
    
  67

.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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Replacing Element Body with jQuery.load(), What Happens To Old Elements?