/    Sign up×
Community /Pin to ProfileBookmark

I don’t use [I]while[/I] often because I don’t understand it and consequently, I crash my machine. Please re-write this using [I]while[/I]

[code=html]

if(document.getElementById(“tray”).hasChildNodes()){
document.getElementById(“tray”).removeChild(document.getElementById(“tray”).getElementsByTagName(“img”))
}
[/code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@ReisNov 01.2009 — I would have tried with a for loop here, try this:

[CODE]if(document.getElementById("tray").hasChildNodes()){
for(var i=0; i<document.getElementsById('tray').getElementsByTagName('img').length; i++) {
document.getElementById("tray").removeChild(document.getElementById("tray").getElementsByTagName("img")[i])
}
}
[/CODE]


didnt test it, so dunno if there are any bugs or typos
Copy linkTweet thisAlerts:
@justinbarneskinauthorNov 01.2009 — ah well, thought I'd try to learn something new but,,

[I]document.getElementsById('tray').innerHTML=""[/I]

works well enough. I don't know, firefox crashes more often than IE on some of my inventions.
Copy linkTweet thisAlerts:
@mrhooNov 01.2009 — A while will continue to loop as long as the condition is true-

just make sure the condition cannot be true forever...

[CODE]var pa= document.getElementById('tray');
var nodes= pa.getElementsByTagName('img');
while(nodes.length){
pa.removeChild(nodes[0]);
}[/CODE]
Copy linkTweet thisAlerts:
@justinbarneskinauthorNov 01.2009 — So, Mrhoo, would that be a better way to do it than innerHTML="" [B]?[/B]

I put in a button to alert(document.images.length)

and I get expected results. Is there memory leak or, why would innerHTML="" be an inferior way to do it?

BTW, my project keeps moving along,, I'm just wishing to employ a whole lot of DOM stuff that I've not bothered with before like,

appendChild(node)

cloneNode(deepBoolean)

removeChild(childreference)

replaceChild(newChild, oldChild)

I wanted to set an onload function and tried this after appendChild(node)

node.setAttribute(attributename, value, [iecaseflag])

But, couldn't get it to work and it was easier to to have document.write('<img src="'+Q[i]+'" onload="nextFunc()"> ')



So I dunno. Guess I'm stuck with methods that will not be supported someday?
Copy linkTweet thisAlerts:
@KorNov 02.2009 — 
I wanted to set an onload function and tried this after appendChild(node)

node.setAttribute(attributename, value, [iecaseflag])

But, couldn't get it to work and it was easier to to have document.write('<img src="'+Q[i]+'" onload="nextFunc()"> ')



So I dunno. Guess I'm stuck with methods that will not be supported someday?
[/QUOTE]


On the contrary, those methods (DOM methods) are the modern one. Can we see you code? Should work, unless you were made a mistake.



innerHTML could be also an alternative. But it depends on the aim of your code. innerHTML is not a standard DOM method and it does not insert always all the elements and their attributes into the DOM tree. If you need a simple display of new elements (and no further DOM manipulation) you may use innerHTML. Otherwise you are forced to use DOM methods.
×

Success!

Help @justinbarneskin 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,
)...