How can I toggle in pure JavaScript?


#1


I am on step 10 of JavaScript and the DOM.


$(.projects).toggle();


I would like to know how to do this in pure JavaScript, without jQuery.
I know it would involve if/else.


#2

A slight correction... className must be quoted in a selector. $('.projects')

Here is one approach...

Something to consider... A class selector is not very specific and will return a collection of nodes which all bear the same class. Something like toggle() will most likely need to affect only one element at any one time.

var projects = document.getElementsByClassName('projects');

There could be multiple nodes in the collection. Is the one we are targeting the first one?

projects[0]

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.