/    Sign up×
Community /Pin to ProfileBookmark

appending multiple items

If i have a webpage with multiple items:

[CODE]
<body>
<div>
<h1></h1>
<p></p>
<p></p>
<h2></h2>
<p></p>
<blockquote></blockquote>
<p></p>
<div></div>
</div>
</body>
[/CODE]

And i want to wrap a div tag around the elements within the first div, how could i go about doing this?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@steveaAug 03.2007 — Untested, but given the site setup as you have it (no ids for any element and that exact setup. ie the div you want to wrap is the first div below body) here's a solution:
[code=php]
var newDiv = document.createElement('div');
var oldDiv = document.getElementsByTagName('body')[0].getElementsByTagName('div')[0];
for(var i=0; i< oldDiv.childNodes.length; i++){
newDiv.appendChild(oldDiv.childNodes[i]);
oldDiv.removeChild(oldDiv.childNodes[i]);
}
oldDiv.appendChild(newDiv);
[/code]
Copy linkTweet thisAlerts:
@p_phreshauthorAug 03.2007 — thanks for the reply. I tried it out but it doesn't seem to work because it's too slow.

The situation is not with the code, it works, but it takes too long. I'm doing this for an SVG XML file. There are possibly 100+ elements in the XML. I want to group them all together, and then rotate them.

I only used HTML elements, because not enough people on here know a whole lot about SVG.

So is there another way to wrap tags around all the elements, instantaneouly? Without having to loop through them?
Copy linkTweet thisAlerts:
@steveaAug 03.2007 — Why yes, yes there is. I try to avoid using innerHTML whenever possible (personal quirk I suppose) but it is significantly faster for something like this:


[code=php]
var newDiv = document.createElement('div');
var newDiv2 = document.creatElement('div'); //This one will replace the old div surround the info (probably a better way to do this but I'm writing this quickly)
newDiv2.innerHTML = document.getElementsByTagName('body')[0].getElementsByTagName('div')[0].innerHTML;
newDiv.appendChild(newDiv2);
document.removeChild(document.getElementsByTagName('body')[0].getElementsByTagName('div')[0]);
document.appendChild(newDiv2);
[/code]


That was written extremely quickly (because after all, I am supposed to be working) but I'll check back in a little while to see if it worked for you or not. I think it's pretty solid though.
Copy linkTweet thisAlerts:
@p_phreshauthorAug 03.2007 — unfortunately 'innerHTML' is not supported by the Adobe SVG DOM.

So i guess that's out.
×

Success!

Help @p_phresh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...