/    Sign up×
Community /Pin to ProfileBookmark

getElementsByClassName workaround?

Hello,
I used getElementsByClassName function and it works greate on new version of Firefox. However, it doesn’t do work on IE and old version of Firefox (2.0)
Is there another approach to this?
function removeRow(element)
{
var className = element.className;
var parentNode = element.parentNode;
var deletedElement;
var deleteElements = document.getElementsByClassName(className);
for(var i=deleteElements.length – 1; i >= 0; i–)
{
deletedElement = deleteElements[i];
parentNode.removeChild(deleteElements[i]);

if(deletedElement.tagName === ‘BR’)
{
break; // exit the loop after we delete the BR because if we keep going we’ll delete other rows.
}
}

}

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@myrtle1908Oct 14.2009 — [code=php]<div class="test">Div 1</div>
<div class="test">Div 2</div>

<script language="javascript">
document.getElementsByClassName = function(cls) {
var found = [];
var rx = new RegExp('(^| )' + cls + '( |$)');
var els = this.getElementsByTagName('*');
for (var i=0; i<els.length; i++) {
var elClass = els[i].className;
if (rx.test(elClass)) {
found.push(els[i]);
}
}
return found;
};

var divs = document.getElementsByClassName('test');
alert(divs.length);
</script>[/code]
Copy linkTweet thisAlerts:
@mrhooOct 15.2009 — You can also return an array of the elements that include any number of space separated class names.

[CODE]function getHTMLClasses(css, root){
root= root || document;
css= css.split(/ +/);
var who, tem, A= [], L2= css.length,
collection= root.getElementsByTagName('*'),
L= collection.length;
while(L){
who= collection[--L];
for(var i= 0, z= css.length; i<z; i++){
if(who.className.indexOf(css[i])== -1){
who= null;
i= z;
}
}
if(who) A[A.length]= who;
}
return A;
}[/CODE]


// eg: getHTMLClasses('hotCss offCss', document.body)
Copy linkTweet thisAlerts:
@rootOct 04.2013 — Here is another method

[CODE]if( !typeof document.getElementsByClassName == 'function'){
Object.prototype.getElementsByClassName = function(cn) {
if (!this) return null;
for (var r=[], e=this.getElementsByTagName('*'), i=e.length; i--;)
if ( e[i].className.indexOf(cn)>-1) r.push(e[i]);
return r;

}
}[/CODE]


Whatever you do, don't post something like this on Stack Overflow, you get berated for posting something that returns an array.

WTF are you supposed to return then... Cream Cakes?


A Node list, if I am not mistaken is a collection of objects and an object is nothing more than an array or typeof array, ergo, no matter which method you try, its going to be an array.
×

Success!

Help @JackBauer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...