/    Sign up×
Community /Pin to ProfileBookmark

getElementsByClassName(‘listItem’); not working in IE

Hi,
I have the following that works in Chrome and and older (recent) version of Firefox.

It marginally works in the latest Firefox and not at all in IE9:

[CODE]
function enlargePic() {
var alldivs = document.getElementsByClassName(‘listItem’);
var el, i = 0, howmany = 0;
while (el = alldivs.item(i++)) {howmany++};
var item = “<?= $Itemid ?>”;

if (howmany >= 1 && item !=360){
var bigPic1 = document.getElementsByClassName(‘myThumb’)[0];
var bigDiv1 = document.getElementsByClassName(‘listItem’)[0];
var subDiv1 = document.getElementsByClassName(‘contentThumbnail’)[0];
bigPic1.setAttribute(“class”, “contentThumbnailBIG”);
bigDiv1.setAttribute(“class”, “listItemBIG”);
subDiv1.setAttribute(“class”, “contentThumbnailBIG”);
}
if (howmany > 17 && item != 360 ){
var bigPic2 = document.getElementsByClassName(‘myThumb’)[12];
var bigDiv2 = document.getElementsByClassName(‘listItem’)[12];
var subDiv2 = document.getElementsByClassName(‘contentThumbnail’)[12];
bigPic2.setAttribute(“class”, “contentThumbnailBIG”);
bigDiv2.setAttribute(“class”, “listItemBIGright”);
subDiv2.setAttribute(“class”, “contentThumbnailBIG”);
}

}
[/CODE]

I’m a bit of a noob with JS so I’m probably missing some syntax somewhere.
Anyway, would appreciate any help you can give.

Thanx!

Mark

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@marklandryauthorApr 19.2011 — Did some digging,

Apparently IE doesn't support "setAttribute"....

Does anyone know of a workaround?
Copy linkTweet thisAlerts:
@MarPloApr 19.2011 — Hy,

You can use the [b]className[/b] property
<i>
</i>if (object.className=='a_clas') { ... }
// or
object.className = 'set_class';
Copy linkTweet thisAlerts:
@marklandryauthorApr 19.2011 — Thanx for your help.

I did the following which works in ff but nothing in IE (I got rid of some of the fluff to make it simpler). It seems that IE has issues with getElementsByClassName as well?

[CODE]function enlargePic() {



var bigPic1 = document.getElementsByClassName('myThumb')[0];
var bigDiv1 = document.getElementsByClassName('listItem')[0];
var subDiv1 = document.getElementsByClassName('contentThumbnail')[0];
bigPic1.className = 'contentThumbnailBIG';
bigDiv1.className = 'listItemBIG';
subDiv1.className = 'contentThumbnailBIG';

}[/CODE]
Copy linkTweet thisAlerts:
@MarPloApr 19.2011 — Thanx for your help.

I did the following which works in ff but nothing in IE (I got rid of some of the fluff to make it simpler). It seems that IE has issues with getElementsByClassName as well?[/QUOTE]


I don't know, but if doesn't work perhaps the IE not support getElementsByClassName().
Copy linkTweet thisAlerts:
@marklandryauthorApr 19.2011 — I don't think it does.

JQuery will do something like this, but I can't figure out how to manipulate a specific element, like the first div that has the class name "myclassname"...
Copy linkTweet thisAlerts:
@rproctor83Apr 19.2011 — Jquery makes it really easy to select by class name... There are also psuedo selectors like $(".className:first-child") which is probably what your looking to do. If you dont wanna use the entire jquery framework, you can take a look at http://sizzlejs.com/ which jquery has built into it, which is the main selector method which allows selecting by class.
Copy linkTweet thisAlerts:
@marklandryauthorApr 19.2011 — THanx!

So the first element would be $(".className:first-child") and the 15th element would be $(".className:fifteenth-child")?

I tried $(".myClass").get(0) for the first element but got nothing....
Copy linkTweet thisAlerts:
@rproctor83Apr 19.2011 — No, the pseudo selectors are only first-child and last-child... There may be another couple, but if you are trying to get a specific element in a list, by a number, then you would use $(".whatever:eq(X)") where X is the zero based index of the element... It may not be zero based, I cant remember. Just look at the jquery docs. Additionaly, there is also a select method of $(".whatever").eq(x)
Copy linkTweet thisAlerts:
@marklandryauthorApr 19.2011 — Success! Thanx a ton - I dug around forever in the api docs and somehow missed the eq(x)

Here's what I ended up with for what it's worth:

[CODE]<script>
$('.myThumb').eq(0).attr('class', 'contentThumbnailBIG');
$('.listItem').eq(0).attr('class', 'listItemBIG');
$('.contentThumbnail').eq(0).attr('class', 'contentThumbnailBIG');
$('.myThumb').eq(12).attr('class', 'contentThumbnailBIG');
$('.listItem').eq(12).attr('class', 'listItemBIGright');
$('.contentThumbnail').eq(12).attr('class', 'contentThumbnailBIG');
</script>[/CODE]


Thanx again
Copy linkTweet thisAlerts:
@KorApr 20.2011 — older versions of IE do not support getElementsByClassName(). But there is a workaround (very much alike JQuery has implemented in the core code):
<i>
</i>function setClassMethod(){
if (!document.getElementsByClassName) {
document.getElementsByClassName = function (cn) {
var rx = new RegExp("(?:^|\s)" + cn+ "(?:$|\s)");
var allT = document.getElementsByTagName("*"), allCN = [], ac="", i = 0, a;
while (a = allT[i=i+1]) {
ac=a.className;
if ( ac &amp;&amp; ac.indexOf(cn) !==-1) {
if(ac===cn){ allCN[allCN.length] = a; continue; }
rx.test(ac) ? (allCN[allCN.length] = a) : 0;
}
}
return allCN;
}
}
}

Which function has to be the first launched onload.
Copy linkTweet thisAlerts:
@XaviaRashNov 23.2011 — Great post really helped me. I am getting an issue with IE, is there any way to change the color of scroll bar of a <div>. Its not working through CSS in IE.
Copy linkTweet thisAlerts:
@KorNov 23.2011 — Great post really helped me. I am getting an issue with IE, is there any way to change the color of scroll bar of a <div>. Its not working through CSS in IE.[/QUOTE]
Don't hijack the posts unrelated with your problem. Open your own Thread, but in the CSS Forum, where it should be.

Anyway, on the contrary, scroll bars can be styled only in IE (and, I think, in Chrome), as far as I know. That is because style the scroll bar is not included in the CSS2 (nor in CSS3) standard.

But you can apply a JavaScript solution. See also:

http://www.hesido.com/web.php?page=customscrollbar
×

Success!

Help @marklandry 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 4.28,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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