PDA

View Full Version : get element by class



Agrajag
05-16-2006, 03:42 AM
how can i use a function to get elements by their className?

thanks

jscheuer1
05-16-2006, 04:03 AM
Here's one from the Switch Content II (http://www.dynamicdrive.com/dynamicindex17/switchcontent2.htm) script in the DD Library:


function getElementbyClass(rootobj, classname){
var temparray=new Array()
var inc=0
var rootlength=rootobj.length
for (i=0; i<rootlength; i++){
if (rootobj[i].className==classname)
temparray[inc++]=rootobj[i]
}
return temparray
}

You call it with the two parameters, the first param is the root object that the elements with your desired class designation are contained in, the second param is the class name you are looking for. It returns an array containing all children of the root object with that class name.

Twey
05-16-2006, 06:31 AM
I've always used this one (http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/).

Agrajag
05-16-2006, 10:08 AM
thanks twey, that is excellent!

Agrajag

mwinter
05-16-2006, 11:18 AM
I've always used this one (http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/).Any code that declares itself to be 'the ultimate' requires reviewing. It's fine, though as commented by other reviewers, escaping hyphens is totally unnecessary.

The Array.prototype.push method substitute is bogus, though.

Mike

Twey
05-16-2006, 11:25 AM
The IE5-support one?
Does IE5 already have one? Perhaps
if(!Array.push) Array.prototype.push = function(val) {
this[this.length] = value;
}would be better anyway.
Any code that declares itself to be 'the ultimate' requires reviewing.Heh, yes. I did have a check through it before using it, though. :)

mwinter
05-16-2006, 11:31 AM
The IE5-support one?
Does IE5 already have one?No, the reason for it is valid: prior to JScript 5.5, there was no push method implemention (though it was specified and implemented elsewhere). The suggested implementation is just bad. I added a comment (http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/#comment-4926) (twice; it barfed on <, unlike here in DDF).

Mike

Twey
05-16-2006, 11:38 AM
if (typeof Array.prototype.push != 'function') {
Array.prototype.push = function(v) {
var i = this.length >>> 0,
j = 0;

while(j < arguments.length) {this[i++] = arguments[j++];}
return this.length = i;
};
}Only one minor, pedantic qualm: you've defined v but never used it :) I seem to remember you doing something like this before, also with a argument named "v," as I recall.

mwinter
05-16-2006, 11:40 AM
Only one minor, pedantic qualm: you've defined v but never used it :)Intentional. :p The Array.prototype.push method is defined as having a length property value of 1. For user-defined functions, the only way to affect the length property is via the formal argument list; the property itself is read only.

Mike

Twey
05-16-2006, 11:44 AM
Aha, I see.
Qualm dismissed. :)

jscheuer1
05-16-2006, 04:44 PM
Mike, while we are discussing some of the less obvious and finer points of your reasons for your coding choices, this came up recently:

Why modulus 1 here:


Array.prototype.swap = function(x, y) {
var t = this[x]; this[x] = this[y]; this[y] = t;
};
Array.prototype.shuffle = function() {
var i = this.length;
while(i--) this.swap(i, Math.floor((Math.random() % 1) * (i + 1)));
};

As far as I can tell, it will have no effect upon what Math.random() returns. If it is to ensure that Math.random() is a number, won't multiplying it by i+1 do that? And aren't all results of the Math object's methods that have no parameters or variable input of any kind numbers anyway?

My thinking is that it crept in from a previous version where it was an important step. If there is a reason, in its present form, I am mystified.

mwinter
05-16-2006, 05:42 PM
Why modulus 1 [...] As far as I can tell, it will have no effect upon what Math.random() returns.I'm sure you asked that when I first posted the code.

An older version of Opera exhibits a bug that causes it to return a number in the range [0,1], not [0,1). That version is probably not present on the Web now, but unless speed is critical (and it usually isn't), it does no harm to leave the expression intact.

Mike

jscheuer1
05-16-2006, 06:16 PM
Originally Posted by jscheuer1
Why modulus 1 [...] As far as I can tell, it will have no effect upon what Math.random() returns.

I'm sure you asked that when I first posted the code.



You are giving my former self too much credit, I had no idea at that time what the % meant in javascript when used as the modulus operator. Thanks for the explanation.

Twey
05-16-2006, 07:11 PM
I had no idea at that time what the % meant in javascript when used as the modulus operator.It has another use?

mwinter
05-16-2006, 07:33 PM
[The modulus (%) operator] has another use?Not in ECMAScript, nor its derivatives.

Mike

Twey
05-16-2006, 08:31 PM
I didn't think so...

jscheuer1
05-16-2006, 09:39 PM
The modulus operator itself of course has no other use in javascript that I am aware of but, the % symbol is often used in strings to represent itself as a literal and as a units label, ex:


var pcentHTML='We currently have '+prcentOn()+'% of our users active.';

and:


dropobj.style.top='50%';


Potentially confusing to any novice coder. We often forget what it is like having such a small amount of knowledge on this subject that even asking an 'intelligent question' is hard but, that is where the vast majority of the world population is.