user7259161
user7259161

Reputation:

Remove <title> tag using javascript

Is there a way to remove title tag using javascript?

I tried to do this

var parent = document.head;
var child = document.title;
parent.removeChild(child);

but it failed because title tag is not a node (console said). I need to force a Wordpress plugin to overwrite the default title tag with a custom one.

Any ideas?

Upvotes: 1

Views: 4653

Answers (4)

Pablo
Pablo

Reputation: 1071

You can use your_element.removeAttribute('title');

Upvotes: 0

Mayank Prajapati
Mayank Prajapati

Reputation: 37

document.getElementsByTagName('title')[0].remove()

Upvotes: 1

glebtv
glebtv

Reputation: 3839

To set document title via JS, use:

document.title = "your title here"

Note that the title you set via JS will not be visible to Google and other search engines.

Upvotes: 0

Quentin
Quentin

Reputation: 943214

The title property just contains a string representation of the title. It doesn't represent the element itself.

You can use any of the usual methods to get the element itself.

document.querySelector("title");
document.getElementsByTagName("title")[0]
// etc

Upvotes: 1

Related Questions