By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

Child nodes

100+
P: 178
Apologies in advance for being an idiot..

I have some javascript objects that are all children of a div "canvas". they all have a class="newObject". I want to setup a loop or something to perform an operation on each one. Can someone make a suggestion please?
Jul 17 '08 #1
Share this Question
Share on Google+
1 Reply


hsriat
Expert 100+
P: 1,654
Recently there's been introduced a new function called getElementsByClassName. But old browsers don't support it. So you can make your function work like this...
Expand|Select|Wrap|Line Numbers
  1. ....
  2. var canvas = document.getElementById('canvas'); //canvas referance
  3. var requiredEle = Array;//array of the elements you need
  4. if (document.getElementsByClassName) //check if the function is supported
  5. requiredEle = canvas.getElementsByClassName('newObject');
  6. else { //else find all the elements with that classname
  7.     var allEle = div.getElementsByTagName("*");
  8.     for(var i in allEle)
  9.     if (allEle[i].className = 'newObject')
  10.     requiredEle.push(allEle[i]);
  11. }
Jul 17 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.