views:

210

answers:

2

Hello all, I currently have a JS function that allows me to load partial content from another page into the current page using jQuery.load()

However I noticed when using this that I get a duplicate ID in the DOM (when inspecting with FireBug)

This function is used in conjuction with a Flash Building viewe so it does not contain any code to retrieve the URL from the anchor tag.

function displayApartment(apartmentID) {
    $("#apartmentInfo").load(siteURL + apartmentID + ".aspx #apartmentInfo", function () {
        //re-do links loaded in
        $("a.gallery").colorbox({ opacity: "0.5" });
    });
}

The code above works just fine in retrieving the content, however it just bugs me that when inspecting with firebug I get something like this.

<div id="apartmentInfo">
    <div id="apartmentInfo">
        <!-- Remote HTML here..... -->
    </div>
</div>

Can anyone please suggest anything on how to remove the duplicate div?

Thanks, Warren

+1  A: 

If it's just that ID that is causing you an annoyance, you could either change that id, or remove the duplicate when the document is loaded:

function displayApartment(apartmentID) {
    $("#apartmentInfo").load(siteURL + apartmentID + ".aspx #apartmentInfo", function () {
        $(this).attr('id', 'somethingElse'); //change the id
        // OR
        $(this).children().unwrap(); // remove the second apartmentId from the DOM completely.

        //re-do links loaded in
        $("a.gallery").colorbox({ opacity: "0.5" });
    });
}

If it's multiple ID's, the only solution I can think of is to prefix all the ID's within the remote page with something, or load the remote page in an IFRAME.

function displayApartment(apartmentID) {
    $("#apartmentInfo").load(siteURL + apartmentID + ".aspx #apartmentInfo", function () {
        $(this).find('*[id]').andSelf().attr('id', function (index, previous) {
            return 'a-random-prefix-' + previous;
        });

        //re-do links loaded in
        $("a.gallery").colorbox({ opacity: "0.5" });
    });
}
Matt
Thanks Matt, I used the unwrap stuff.$(this).children().unwrap();
Warren Buckley
+2  A: 

a way around it maybe

function displayApartment(apartmentID) {
    var parent = $("#apartmentInfo").load(siteURL + apartmentID + ".aspx #apartmentInfo", function () {
        parent.children(':first').unwrap();
        //re-do links loaded in
        $("a.gallery").colorbox({ opacity: "0.5" });
    });
}
Reigel
Thanks Reigel for this, i just went with matt's version of unwrap by using $(this)
Warren Buckley
okay cool... :)
Reigel