Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
I had fun making this. It was hard as hell, cause I made on function, that wasn't CB. :P 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

Nate

Nate Avatar

**
Official Member

85


July 2007
Nice job. these should definitely be already in javascript .
document.getElementByAlt('') XD

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
HEh, I could try making that. :P It'd be fairly easy though.

hpmad

hpmad Avatar



858


September 2005
Nice one :D I never knew there wasn't a getElementByClassName O_o

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
There wasn't. :P 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

crazynarutard

crazynarutard Avatar

*****
Senior Studio Member

1,470


August 2005
cddude229 said:
There wasn't. :P 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 :P

Zero Tolerance

Zero Tolerance Avatar

*
New Member

21


October 2005
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:

Not your prototypes
You should be very careful about adding extra properties to object prototypes. These are examples of the most problematic prototype modifications, which affect all variables, as they inherit from Object:

Object.prototype.newProperty = 'foo';
Object.prototype.newMethod = function () { ... };Several scripts use the for( var x in y ) loop, and if you modify the prototypes like this, they will end up indexing properties that they did not expect, causing unexpected behaviour. With many menu scripts, this actually causes the entire scripts to fail. If you need to add to the prototype like this, add it just before you use it, do what you need, then delete it again so that it does not affect any more scripts.


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

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
:P 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. :P Anyway, thanks for the help on that ZT.

peter
Guest
cddude229 said:
:P 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. :P 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

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Yeah, I found out from experience. :P

And ZT posted a code that he says works, but I have yet to have a chance to test.s

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
I went ahead and finally tested ZT's, and it doesn't seem to work with IE. :P

Zero Tolerance

Zero Tolerance Avatar

*
New Member

21


October 2005
Really? Worked fine for me in IE and FF, what error (if any) did you get?

- Zero Tolerance

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
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

hey

hey Avatar
j00 n00b. =P

**
Official Member

50


October 2005
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

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
That doesn't work for me either... says getClass() needs an argument :P Meaning, the arguments[0] is tracing back to it instead of to getElementsByClassName.

newBookmarkLockedFalling