Skip to content
Related Articles

Related Articles

Improve Article
How to get title of current HTML page using jQuery ?
  • Last Updated : 02 Mar, 2020

Suppose you have given an HTML page and the task is to get the title of an HTML page with the help of only jQuery. There are two approaches that are discussed below:

Approach 1: The $(‘title’) jQuery selector is used to select the title element of the document and we can use text() method on the selector to get the title of the document.

  • Example:




    <!DOCTYPE HTML> 
    <html
      
    <head
        <title
            Get title of current HTML page
        </title>
        <script src
        </script>
        <style>
            body {
                text-align:center;
            }
            #gfg{
                color: green; 
                font-size: 24px; 
                font-weight: bold;
                  
            }
        </style>
    </head
      
    <body
        <h1 style="color:green;"
            GeeksforGeeks 
        </h1
        <b>
            Click on button to get HTML document's title
        </b
        <br><br>
        <button onClick="GFG_Fun()"> click here</button
        <p id="gfg"
        </p
          
        <script
            var down = document.getElementById('gfg'); 
          
            // Main function
            function GFG_Fun() { 
                down.innerHTML = $('title').text();
            
        </script
    </body
    </html>                    
  • Output:

Approach 2: The $(document) jQuery selector is used to select the HTML document and we can use attr() method on the selector to get the title of the document by passing ‘title’ as argument.

  • Example:




    <!DOCTYPE HTML> 
    <html
      
    <head
        <title
            Get title of current HTML page
        </title>
        <script src
        </script>
        <style>
            body {
                text-align:center;
            }
            #gfg{
                color: green; 
                font-size: 24px; 
                font-weight: bold;
                  
            }
        </style>
    </head
      
    <body
        <h1 style="color:green;"
            GeeksforGeeks 
        </h1
        <b>
            Click on button to get HTML document's title
        </b
        <br><br>
        <button onClick="GFG_Fun()"> click here</button
        <p id="gfg"
        </p
          
        <script
            var down = document.getElementById('gfg'); 
          
            // Main function
            function GFG_Fun() { 
                down.innerHTML = $(document).attr('title');
            
        </script
    </body
    </html>                    
  • Output:



My Personal Notes arrow_drop_up
Recommended Articles
Page :