Green

Sunday, April 14, 2013

A Beginner Question In Javascript

getElements -- PLURAL, returns an array. you should either loop that array hitting all .first on the page, or use a unique ID with getElementsById...

I generally avoid getElementsByClassName since legacy browsers (old IE) don't support it.

Also, are you trying to ADD a class or replace it? This:
className= "active";

REPLACES it -- so it would no longer be 'first'. className is a string, so to add a new class you'd do:
className += ' active';

to ADD that class.

Though I'm not sure why you're playing with classes so much from Javascript... I mean, there are reasons to do it, but usually that's for goofy interactive crap that makes pages LESS useful.

But to be fair, I say that about 90%+ of the stuff people use Javascript to do on pages.


View the original article here

0 comments:

Post a Comment