Tuesday, 29 August 2017

what is $(Document).ready() in Jquery ?

$(document).ready() is a Jquery Event.It fires when DOM is loaded and ready to manipulated by script.This is the earliest point in the page load process where the script can access pages HTML dom. This event fires before the images,css etc are fully loaded.

In the below code we use Jquery inside the Content page so we write '#<%= Button1.ClientID %>' on the place of   '# Button1' because all the elements access on content page by ClientID.

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="GridviewData.aspx.cs" Inherits="GridviewData" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
    <script src="Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
       $(document).ready(function(){
            $('#<%= Button1.ClientID %>').click(function () {
                alert('Hello Sudhir Singh');
            }

                );
        }
         
        );
    </script>
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" />
     
    </div>
</asp:Content>

NOw replace the

     $(document).ready(function(){
            $('#<%= Button1.ClientID %>').click(function () {
                alert('Hello Sudhir Singh');
            }

                );
        }
         
        );

Code with following code  this will not work properly because of we remove the $(documnet).ready() function.when you click on button you don't get alert message because

   
            $('#<%= Button1.ClientID %>').click(function () {
                alert('Hello Sudhir Singh');
            }

                );
        }
         
     


No comments:

Post a Comment

Creating First WEB API Project from scratch ?

  For creating Web API project open your Visual Studio Editor and Create New Project as shown in below figure. Now you have to select  ASP.N...