Add Class to Object on Page Load

Miles Pfefferle picture Miles Pfefferle · Feb 14, 2011 · Viewed 105.2k times · Source

Basically I want to make this:

<li id="about"><a href="#">About</a>

Into this when the page loads:

<li id="about" class="expand"><a href="#">About</a>

I found this thread, but am not so good with javascript and couldn't adapt it: Javascript: Onload if checkbox is checked, change li class

Answer

Jacob Relkin picture Jacob Relkin · Feb 14, 2011

This should work:

window.onload = function() {
  document.getElementById('about').className = 'expand';
};

Or if you're using jQuery:

$(function() {
  $('#about').addClass('expand');
});