Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get title tag using jQuery?

I have some html in string form.

var html = "
<html>
  <head>
    <title>
      Some Text
    </title>
  </head>
  <body>
    <h1>
      My First Heading
    </h1>
    <p>
      My first paragraph.
    </p>
  </body>
</html>
";

It has title tag inside it. How can I get the text inside title tag using jquery or javascript?

like image 766
Sushil Kumar Avatar asked May 15 '14 14:05

Sushil Kumar


People also ask

How to get button title in jQuery?

You can get it by using . text() , $('button'). text();

How to select title attribute in jQuery?

jQuery [attribute|=value] Selector The [attribute|=value] selector selects each element with a specified attribute, with a value equal to a specified string (like "en") or starting with that string followed by a hyphen (like "en-us"). Tip: This selector is often used to handle language attributes.

How do you make a page title?

Again, on Windows, you can select Ctrl + F and then type “title” to quickly find the Title. That's all there is to it.


2 Answers

In JavaScript, simply

document.title

For sample code

<html>
    <head>
        <title>My Page Title</title>
    </head>
    <body>
        <p id="demo"></p>
        <script>
            document.getElementById("demo").innerHTML = document.title;
        </script>
    </body>
</html>
like image 110
saiid Avatar answered Oct 12 '22 23:10

saiid


Just try with:

var title = $(html).filter('title').text();
like image 21
hsz Avatar answered Oct 12 '22 23:10

hsz