Joshua Rajandiran Joshua Rajandiran - 1 year ago 81
CSS Question

How to add class to a html file loaded via jquery?

I've included my header & footer at separate html files using

from jQuery. My jQuery code is below:


Right now, as I navigate to another page, I want to add an
class to one of my header/footer links but I can't seem to achieve that. Anyone can help me?

I've tried something like this but it didn't work:


Answer Source

Use complete callback of jQuery.load method.

.load( url [, data ] [, complete ] )

When you are invoking addClass method for element, element does not exist in DOM, callback function is invoked when external file is loaded in specified element

$(function() {
  $("#header").load("page-component/header.html", function() {
  $("#footer").load("page-component/footer.html", function() {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download