|
I had fun making this. It was hard as hell, cause I made on function, that wasn't CB. So I basically started from scratch to get this. It works in IE and FF, and if anyone can test it in other browsers for me, that'd be great. Paste this into a notepad file, save it as a .html, and open it into your browser for an example of the code at work. <html> <body> <font class="class">Testing</font><br /> <font class="notclass">Testing2</font><br /> <font class="class">Testing3</font> </body> </html>
<script> document.getElementsByClassName = function(){ if(document.hasChildNodes && arguments[0]){ var data = new Array(); for(a=0;a<document.getElementsByTagName("*").length;a++){ if(document.getElementsByTagName("*")[a].className == arguments[0]){ data.push(document.getElementsByTagName("*")[a]); } } return data; } }
document.getElementsByClassName("class")[0].style.display = "none"; document.getElementsByClassName("class")[1].innerHTML = "Dude"; </script> For just the code, use this. <script> document.getElementsByClassName = function(){ if(document.hasChildNodes && arguments[0]){ var data = new Array(); for(a=0;a<document.getElementsByTagName("*").length;a++){ if(document.getElementsByTagName("*")[a].className == arguments[0]){ data.push(document.getElementsByTagName("*")[a]); } } return data; } } </script>
Last Edit: Oct 17, 2005 4:19:52 GMT by Chris
|
|
|
|
|
Nice job. these should definitely be already in javascript . document.getElementByAlt('') XD
|
|
|
|
HEh, I could try making that. It'd be fairly easy though.
|
|
|
|
|
Nice one I never knew there wasn't a getElementByClassName
|
|
|
|
There wasn't. That's the whole point of this code. I made a function that creates getElementsByClassName, but it only works for "document".... I've been trying to figure out a way to make it work on anything, but it only works in FF so far for that.s
|
|
|
|
|
There wasn't. That's the whole point of this code. I made a function that creates getElementsByClassName, but it only works for "document".... I've been trying to figure out a way to make it work on anything, but it only works in FF so far for that.s Leave it to IE for being a *****.....again
|
|
|
|
You can use the Object.prototype handler for adding in your own constructer functions, I had a look around, a lot of people don't seem to agree with it, I'll quote: But, not sure if this has that affect really, heres the modified version of your code so it supports any parent object: Object.prototype.getElementsByClassName = function() { Elements = this.getElementsByTagName('*') ElementsFound = new Array if (arguments[0]) { for (e = 0; e < Elements.length;e++) { if (Elements[e].className == arguments[0]) { ElementsFound.push(Elements[e]) } } }
return ElementsFound } - Zero Tolerance
Last Edit: Oct 31, 2005 7:33:11 GMT by Zero Tolerance
|
|
|
|
|
I'd been thinking about it recently, and thought object might work, but I never tried. I was using "Node" when I tried to make it originally. Anyway, thanks for the help on that ZT.
|
|
|
|
peter
Guest
|
I'd been thinking about it recently, and thought object might work, but I never tried. I was using "Node" when I tried to make it originally. Anyway, thanks for the help on that ZT. Only problem with prototyping to the Node is that IE doesn't allow Javascript to access DOM constructors. Something I hope changes in IE7. Check this out - Click Me
|
|
|
|
Yeah, I found out from experience. And ZT posted a code that he says works, but I have yet to have a chance to test.s
|
|
|
|
|
I went ahead and finally tested ZT's, and it doesn't seem to work with IE.
|
|
|
|
|
Really? Worked fine for me in IE and FF, what error (if any) did you get?
- Zero Tolerance
|
|
|
|
|
No error. It just didn't work, like it didn't work when I used Node..... Could just be that computer. Its strange in FF, maybe the same in IE? I'll try it now on this one. Edit: No luck. I'm using this. <html> <body> <font class="class">Testing</font><br /> <font class="notclass">Testing2</font><br /> <font class="class">Testing3</font> </body> </html>
<script> Object.prototype.getElementsByClassName = function() { Elements = this.getElementsByTagName('*') ElementsFound = new Array if (arguments[0]) { for (e = 0; e < Elements.length;e++) { if (Elements[e].className == arguments[0]) { ElementsFound.push(Elements[e]) } } }
return ElementsFound }
document.getElementsByClassName("class")[0].style.display = "none"; document.getElementsByClassName("class")[1].innerHTML = "Dude"; </script>
Last Edit: Nov 4, 2005 1:13:49 GMT by Chris
|
|
|
|
|
It worked for me... Url: www.talesrpg.com/test/getclass.html<html> <head> <title>Meh</title> <script type="text/javascript"> function getClass(){ Object.prototype.getElementsByClassName = function() { Elements = this.getElementsByTagName('*') ElementsFound = new Array if (arguments[0]) { for (e = 0; e < Elements.length;e++) { if (Elements[e].className == arguments[0]) { ElementsFound.push(Elements[e]) } } } return ElementsFound } document.getElementsByClassName("class")[0].style.display = "none"; document.getElementsByClassName("class")[1].innerHTML = "Dude"; } </script> </head> <body> <font class="class">Testing</font><br /> <font class="notclass">Testing2</font><br /> <font class="class">Testing3</font> <script type="text/javascript"> getClass(); </script> </body> </html>
|
forums.talesrpg.com
|
|
|
|
That doesn't work for me either... says getClass() needs an argument Meaning, the arguments[0] is tracing back to it instead of to getElementsByClassName.
|
|
|
|