Online Snippets Library

Set page title using jQuery

Some time we need to change/set page title dynamically.we can achieve this by attr method of jQuery.

attr() method is used to sets or returns attributes and values of selected elements.

Syntax

$(selector).attr(attributeName) $(selector).attr(attributeName,value) $(selector).attr(attributeName,function(index,currentvalue)) $(selector).attr({attributeName:value, attributeName:value,...})

Example

HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
<title>set page title using jQuery </title>
</head>
<button>change title</button>
</html>
Javascript
$(document).ready(function(){
$("button").click(function(){
$(document).attr("title", "My Title");
}); 
});