Change Font Family on select Drop Down – Simple jQuery script!

jquery-theonlytutorials

 

Here is another great tutorial – How to change font family dynamically using jQuery!!

I have used a drop down list of font-families. OnChange of the drop down box will change the font type of the document.

Go to the bottom of the page to see the DEMO or DOWNLOAD the Source Code.

Here is the jQuery part, very simple isn’t it?

$(document).ready(function() {
  $("#fontsense").change(function () {
    var str = "";
    $("#fontsense option:selected").each(function () {
	str += $(this).text() + " ";
    });
    $("p").css('font-family', str);
  });     
});

Here is the Entire Code (copy paste the below example and try yourself):

 <!doctype html>
<html>
<head>
	<title>Simple jQuery Change Font Family from drop down</title>
	<script src="https://code.jquery.com/jquery-latest.min.js"></script>
        <!-- i have used some google fonts too -->
	<link href='https://fonts.googleapis.com/css?family=Merienda+One|Aldrich|Nothing+You+Could+Do|Crete+Round|Karla:400,700|Original+Surfer|Salsa|Marmelad|Averia+Sans+Libre:700|Righteous|Sancreek|Alegreya+SC:900' rel='stylesheet' type='text/css'>
	<script>
	 $(document).ready(function() {
		$("#fontsense").change(function () {
			var str = "";
			$("#fontsense option:selected").each(function () {
				str += $(this).text() + " ";
			});
			$("p").css('font-family', str);
		});     
	});    
	</script>
	<style>
		#sample{margin: 0 auto; width: 900px; border: 1px solid gray; padding: 10px;}
	</style>
</head>
<body>
	<h2>Blog.theonlytutorials.com</h2>
	<div id="sample">
		Change Font: <select id="fontsense" >
			<option>Select Font</option>
			<!--default fonts-->
			<option>Arial</option>
			<option>Comic Sans MS</option>
			<option>Trebuchet MS</option>
			<option>Verdana</option>

			<!--google fonts-->
			<option>Averia Sans Libre</option>
			<option>Righteous</option>
			<option>Sancreek</option>
			<option>Alegreya SC </option>
			<option>Merienda One</option>
			<option>Aldrich</option>
			<option>Nothing You Could Do</option>
		  </select>
		<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
	</div>
</body>
</html>

demoDownload

One thought on “Change Font Family on select Drop Down – Simple jQuery script!

Leave a Reply

Theme: Overlay by Kaira
Agurchand