By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,174 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.

get the tags i want.. efficiently

100+
P: 107
Hey,

im using getElementsBytagName...

Expand|Select|Wrap|Line Numbers
  1. foo = document.body.getElementsByTagName("div")
But say i want to get all the divs and all the td tags, or any number of specified tags, and store them in the one variable...

What would be the most efficient way to do this?

can you do this:
Expand|Select|Wrap|Line Numbers
  1. foo = document.body.getElementsByTagName("div")
  2. foo += document.body.getElementsByTagName("span")
  3. foo += document.body.getElementsByTagName("td")
????

probably not i think


Cheers

Andy
Aug 5 '08 #1
Share this Question
Share on Google+
2 Replies


acoder
Expert Mod 15k+
P: 16,027
You could try using getElementsByTagNames() (note the 's').
Aug 5 '08 #2

rnd me
Expert 100+
P: 427
with a little overhead, you could code it quite simply.

library: tags (gets a true array of tag names)


Expand|Select|Wrap|Line Numbers
  1. function tags(elm, tid) {function obValsl(ob) {try {if (ob && ob.length) {var r = [], i = 0, mx = ob.length;for (var z = 0; z < mx; z++) {r[z] = ob[z] || undefined;}}} catch (yy) {alert(yy);}return r;}
  2. if (tid) {if (elm.split) {elm = el(elm);}return obValsl(elm.getElementsByTagName(tid));}return obValsl(document.getElementsByTagName(elm));};
  3.  
  4. //then you code is simple:
  5. myTags = tags("div").concat(tags("span")).concat(tags("td"));
  6.  
  7.  
Aug 5 '08 #3

Post your reply

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