tags:

views:

1573

answers:

4

I have a div with the id of "secondHeader" and I want to replace that entire div with another div with the same id of "secondHeader" but instead of replacing it , it just adds the loaded div inside the first one.

$("#secondHeader").load("/logged-in-content.html #secondHeader");

This is what happens...

<div id="secondHeader"><div id="secondHeader"></div></div>

What I want to happen is the secondHeader div from the ajax load to totally replace the secondHeader in the initial page.

I know it sounds dumb, but here's what I'm trying to accomplish...When a user is not logged in, they see a non-logged in header. I am using ajax to allow the person to log into the site and I want to replace the non-logged in header with the logged in one via ajax.

I have tried everything I know such as...

$("#secondHeader").replaceWith($("#secondHeader").load("/logged-in-content.html #secondHeader"));

...and using .remove() before hand...

Any ideas?

+2  A: 

Could you refine your selector in the load() method?

For example,

$("#secondHeader").load("/logged-in-content.html #secondHeader > *");

This way, you're not grabbing the div itself, you're grabbing its contents.

Funka
Thanks for your suggestion. I actually tried that, but it comes back really funky and not all as one cohesive div.
vipergtsrz
how about loading the remote content into a temporary div. Then on the callback, after doing this, simply relocate the contents as desired. This would "work", but probably not an ideal or terribly elegant approach. The key will be to do this in the "load" method's callback, to allow time for the content to finish retrieving. I looked at your original post again and perhaps this timing issue is what is causing your original ideas not to work for you...
Funka
Hmmm, interesting that the `#secondHeader > *` selector doesn't work for you. I just today had a similar need (I want just the contents of the remote div, not the enclosing div itself) and thought I'd give this a try. It works great in my case, exactly as expected.
Funka
Maybe the probleme was due to the fact that, using load we will get only the innerhtml of the loaded page. Which means that script tags will not be loaded. At least with chrome.
BenZen
+1  A: 

$.load isn't really the best choice here since that function's intended to just fill in the contents of a div, as you've seen. You can just use $.get instead and set the callback function to replace your original div, or change logged-in-content.html to exclude the div.

Also be aware that as a Javascript-based solution, if your users look at the source, they'll see that they can get access to logged-in-content.html by just typing it in their address bar if you're not securing it somehow else.

Randy
I would recommend keeping your headers in separate files and loading them as needed.
Olivieri
It's the same header, and it uses a loginview asp.net control. I don't think it would matter much if they were in different files cause I would have the same problem.I will try to the $.get method, but what I really liked about the $.load was that I could select only the part I wanted/needed and I don't think I can use that functionality with $.get
vipergtsrz
A: 

You want to wrap in div before inserting it.

$.ajax({
    url: "/logged-in-content.html",
    success: function(response){
        var loadedheader = $("<div/>").append(
        response.replace(/<script(.|\s)*?\/script>/g, "")
        ).find('#secondHeader > *').html();
        $("#secondHeader").append(loadedheader);
    }
});
Jourkey
Thanks for the code Jourkey. I just tried it and it doesn't seem to do what I need it to. It just takes individual elements and adds them one at a time in a non-hierarchical fashion so it doesn't keep the same dom structure as I need it to have. I'm still trying to find a solution.
vipergtsrz
A: 

Can you add a container DIV around your "secondHeader" div? Then you'd use:

$('#secondHeaderContainer').load('/logged-in-content.html #secondHeader');
danblaker