Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Class to Object on Page Load

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

like image 879
Miles Pfefferle Avatar asked Feb 14 '11 17:02

Miles Pfefferle


2 Answers

This should work:

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

Or if you're using jQuery:

$(function() {
  $('#about').addClass('expand');
});
like image 98
Jacob Relkin Avatar answered Oct 21 '22 20:10

Jacob Relkin


I would recommend using jQuery with this function:

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

This will add the expand class to an element with id of about when the dom is ready on page load.

like image 39
Swaff Avatar answered Oct 21 '22 18:10

Swaff