/    Sign up×
Community /Pin to ProfileBookmark

className vs class

Hi,
I’m new to the forum! 😀

I’m working on something at the moment and just had a little trouble getting something working. I solved the problem but I don’t know what I was doing wrong so I thought I’d ask on here.

I have an array of DOM elements i got using getElementsByTagName().
One each of those elements the className variable is undefined, but getAttribute(‘class’) works ok.

The elements are table cells: <td class=”myClass”></td>
The problem occurred in FF 3.0.3 but i didnt have any other browsers to test on.

I’m not particularly familiar with DOM stuff yet so does anybody know why this happens?

Thanks
Joe

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Nov 12.2008 — Well normally the problem is getAttribute('class'). It's very problematic, you never really should use it. I've never seen .className not work, do you happen to still have the code.
Copy linkTweet thisAlerts:
@mrhooNov 12.2008 — I have occasionally seen this in dom browsers when the class was [B]set [/B]using setAttribute('class',string). (This won't work yet in IE, nor getAttribute('class'))

var cname= elem.className || elem.getAttribute('class');
Copy linkTweet thisAlerts:
@mintedjoauthorNov 12.2008 — heres the thing that wasn't working

Its not the exact code but it still gives me the PHAIL

[CODE]var tableCells = outputTable.getElementsByTagName('td');
for(var i = 0; i < tableCells.length; i++){
c = tableCells[i];
alert(c.getAttribute('class')); // Works
alert(c.className); // Displays undefined
}[/CODE]


If something I'm doing is stupid then please do tell.

Thanks for the replies.

Joe

EDIT: I didn't use setAttribute(), but the code is generated in js and then posted using innerHTML. Do you think this might be why?

It still doesn't make sense to me because if the class attribute is set then surely className should work :-S
Copy linkTweet thisAlerts:
@DecafMatanNov 12.2008 — This doesn't seem probable, .className has worked for ages on every browser. Could you post the javascript file?
×

Success!

Help @mintedjo 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 5.17,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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