Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Getelementbyclass, need a little help with this
reatum
post Oct 11 2005, 09:59 PM
Post #1


Member [Level 1]
****

Group: Members
Posts: 63
Joined: 3-October 05
Member No.: 12,515



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


This post has been edited by rejected: Oct 13 2005, 04:10 AM
Go to the top of the page
 
+Quote Post
reatum
post Oct 19 2005, 01:27 AM
Post #2


Member [Level 1]
****

Group: Members
Posts: 63
Joined: 3-October 05
Member No.: 12,515



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.
Go to the top of the page
 
+Quote Post
Trap FeedBacker
post Apr 22 2008, 09:07 AM
Post #3


Guest Feedbacks
***************

Group: Members
Posts: 2,360
Joined: 21-September 07
Member No.: 50,369



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]
Go to the top of the page
 
+Quote Post
ivenms
post May 3 2008, 04:50 PM
Post #4


Member [Level 1]
****

Group: Members
Posts: 51
Joined: 6-August 06
Member No.: 27,912



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.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics


 



- Lo-Fi Version Time is now: 16th May 2008 - 07:52 PM