0

I'm using JavaScript to copy a specific div from a page into a new page. I need to remove the ID attributes for each table in the new page.

It seems that since I'm copying content from the first page, I can filter out the IDs from the string before it is written to the second page. Can jQuery take a variable as its 'focus'? Instead of manipulating the entire DOM, manipulate a particular string?

I have a non-working version of what I'm talking about:

var currentContent =  window.open('','currentContentWindow');
var htmlToCopy = '<html><head><title></title></head><body>' + window.frames[0].document.getElementById('PageContentPane').innerHTML + '</body></html>';

        $("table", htmlToCopy).removeAttr('id');

        currentContent.document.open();
        currentContent.document.write(htmlToCopy);
        currentContent.document.close();
Eric Di Bari
  • 3,707
  • 7
  • 38
  • 48

3 Answers3

2

You need to create a jQuery object by calling $(html), manipulate it, then get the HTML back by calling html().

For example:

var currentContent =  window.open('','currentContentWindow');
var htmlToCopy = '<html><head><title></title></head><body>' + window.frames[0].document.getElementById('PageContentPane').innerHTML + '</body></html>';

var newStructure = $("<div>" + htmlToCopy + "</div>");
newStructure.find("table").removeAttr('id');

currentContent.document.open();
currentContent.document.write(newElements.html());

The <div> element allows me to get its inner HTML and get the HTML you're looking for.

SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
1

Who not just remove ID= as a string and forget DOM manipulation all together?

Diodeus - James MacFarlane
  • 110,221
  • 32
  • 151
  • 174
0

First make the string a jQuery object, then work with it:

htmlToCopy = $(htmlToCopy).find("table").removeAttr('id').end().html();
Flo Edelmann
  • 2,523
  • 1
  • 18
  • 33