Skip to main content

One of the problem that could arise with our previous jQuery tutorial is that the javascript line will be executed before any of HTML DOM elements are created. So the code will try to access the DOM element that doesn't exist yet.

The only way to solve the issue is to delay the JavaScript execution untill all DOM elements are created on the page.

We can achieve this goal using $(document).ready() function after the DOM elements are loaded and ready as in the code below:

$(document).ready(function() {
  $('p').addClass('newclass');
});

- Advertisement -
- Advertisement -