Jump to content
xisto Community
Sign in to follow this  
reatum

Getelementbyclass need a little help with this

Recommended Posts

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.

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

Edited by rejected (see edit history)

Share this post


Link to post
Share on other sites

I figured it out...

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.

Share this post


Link to post
Share on other sites

even a bit of code

Getelementbyclass

 

Another one from some site

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;	}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.