Jul 25, 2008

Getelementbyclass - need a little help with this

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > HTML, XML etc..

free web hosting

Getelementbyclass - need a little help with this

reatum
I am trying to write a function that will allow me to manipulate all of the elements on a page with the same class. Something like getElementByClass. If anyone has any ideas, please send them this way.

Thanks in advance.
ReAtum

This is what I have, but it is giving me a headache. When I define a classname, and have one element with that classname on the page, it alerts me that it sees 8 elements with that classname. I am @ a loss.
CODE

var elArr = new Array();
function getElementByClassName(classname) {
var allEl = document.all;
for (var i=0; i < allEl.length; i++) {
if (allEl[i].className == classname) {
elArr[i] = allEl[i];
}
}
alert (elArr.length);
}


if anyone has any ideas, I would appreciate it.
and before anyone bugs on me, it is only for IE, so that is why I only have doc.all.

Notice from BuffaloHELP:
Please make sure your title sets the core message of your post. "Need a little hlep with this" is not recommended as the topic title. Switching your description as your title.

Notice from Rejected:
Added stuffs

 

 

 


Reply

reatum
I figured it out...
CODE

function getElementsByClassName(classname) {
    if (document.getElementsByTagName) {
         var els = document.getElementsByTagName("*");
         var c = new RegExp('/b^|' + classname + '|$/b');
         final = new Array();
         var n=0;
         for (var i=0; i < els.length; i++) {
              if (els[i].className) {
                   if(c.test(els[i].className)) {
                   final[n] = els[i];
                   n++;
                   }
              }
         }
         return final;
    } else{return false;}
}

I changed to getElementsByTagName to make it crossbrowser compatible, and used the regex to allow for the use of multiple classnames on an element.
If anybody knows how to make this function work like a method (i.e. document.getElementsByClassName(classname)) please help. Also the RegExp is a little weak and can get confused, so if you can improve on that also, please, be my guest.

 

 

 


Reply

iGuest
even a bit of code
Getelementbyclass

Another one from some site
[code]
Function getElementsByClass(searchClass,node,tag) {
var classElements = new Array();
if (node == null) node = document;
if (tag == null) tag = '*';
var els = node.GetElementsByTagName(tag);
var elsLen = els.Length;
var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
for (I = 0, j = 0; I < elsLen; I++) {
if (pattern.Test(els[I].ClassName)) {
classElements[j] = els[I];
j++;
}
}
return classElements;
}
[/code]

Reply

ivenms
Thanks for code buddies....

The codes listed over here are very helpful for me. Iam looking for some similar code like this for a long tiiime. Thanks once more.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. getelementbyclass in ie - 27.58 hr back. (1)
  2. return getelementbyclass(" - 34.29 hr back. (1)
  3. get element by class - 7.20 hr back. (4)
  4. getelementbyclass - 0.09 hr back. (77)
Similar Topics

Keywords : getelementbyclass


    Looking for getelementbyclass

Searching Video's for getelementbyclass
advertisement



Getelementbyclass - need a little help with this



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE