How to get title of current HTML page using jQuery ?

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= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">     </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>                    
<!DOCTYPE HTML> <html>   <head>     <title>         Get title of current HTML page    </title>    <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">     </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= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">     </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>                    
<!DOCTYPE HTML> <html>   <head>     <title>         Get title of current HTML page    </title>    <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">     </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:
CSS-Misc
HTML-Misc
jQuery-Misc
CSS
HTML
JQuery
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.