javascript

[Jquery] Waiting for the DOM

When you load a page, you need to make sure that all elements (HTML elements) have been loaded before making any change. This is because the main idea of jquery is to find elements in the document using CSS selectors.

We will get unexpected results if we apply those selectors before the browser has managed to load and process the entire HTML because some elements we are looking for may not be available yet.

There are 2 ways to do this:

  • Put the script at the end of the body section of HTML document
  • Use the jquery ready function
/// <reference path="jquery-3.2.1.js" />
$(document).ready(function() {
    //
});

 

Note: We need to add the reference to jquery by dragging the jquery file into the editor area of the jquery file we are going to use jquery. It will be commented out but ASP.NET still process the line.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s