Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to change the font family using JQuery?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.39k
    Comment on it

    In this blog, we will see how we can change the font family of text through jQuery. In this blog we have two div in the left div we show the text and in the right section, we make a selectbox( ) from which we select the particular font whatever you want. We have stored the text of select box in a variable and the n provided that value of a variable to the CSS of the text.

    The HTML goes like this:

    <!DOCTYPE html>
    <html>
    <head>
    	<title>jQuery task</title>
    	<link rel="stylesheet" type="text/css" href="css/style.css">
    	<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    	<link rel="stylesheet" href="https://fortawesome.github.io/Font-Awesome/assets/font-awesome/css/font-awesome.css">
    	<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    	<script type="text/javascript" src="js/bootstrap.min.js"></script>
    </head>
    <body>
    
    	<section>
    	<div class="clearfix">
    		
    		<div class="midSection clearfix" id="preview">
    			<p class="col-md-2"></p>
    			<div class="col-md-8 ">
    		
    				<div class="text-center textBlock">
    					<p class="heading">Lecco</p>
    					<p class="subHeading">Inspiring wordpress theme for everyone</p>
    					<p><a href="#">start</a></p>
    				</div>
    				<p class="text-center">
    					<a href="http://file:///D:/jquerytask/jquerytask/jquery.html#preview">PREVIEW</a>
    				</p>
    			</div>
    			<p class="col-md-2 text-center">
    				<span class="border"><a href="#"><i class="fa fa-twitter" aria-hidden="true"></i></a></span>
    				<span class="border"><a href="#"><i class="fa fa-facebook" aria-hidden="true"></i></a></span>
    			</p>
    		</div>
    		<div class="rightSection">
    			<p>GLOBAL<span class="cross pull-right">x</span></p>
    			<div>
    				<!-- Nav tabs -->
    				<ul class="nav nav-tabs" role="tablist">
    					
    					<li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
    				
    				</ul>
    				<!-- Tab panes -->
    				<div class="tab-content">
    					
    					<div role="tabpanel" class="tab-pane" id="profile">
    						<div class="fontFamily">
    							<p>Heading</p>
    							 <select>
    							  	<option value="Conv_GothamMedium">Conv_GothamMedium</option>
    							  	<option value="harabara_mais_bharabaramaisBd">harabara_mais_bharabaramaisBd</option>
    							  	<option value="adamnormal">adamnormal</option>
    							  	<option value="silent_reactionregular">silent_reactionregular</option>
    							</select> 
    						</div>
    
    					</div><!-- profilediv -->
    
    				</div><!-- tab-content -->
    			</div><!-- tabdiv -->
    		</div><!-- rightSection -->
    	</div>
    	</section>
    	
    </body>
    </html>

     

    The code for the JQuery is given below:

    	<script type="text/javascript">
    		$(document).ready(function(){
    			
    				$( "select" ).on( "click", function() {
    				$y = $(".fontFamily select option:selected").text();
    				$(".midSection .textBlock .heading").css("font-family",$y);
    			});
    				
    		});
    	</script>

     

    The code for CSS goes like this:

    @font-face {
    	font-family: 'Conv_GothamMedium';
    	src: url('../fonts/GothamMedium.eot');
    	src: local(''), url('../fonts/GothamMedium.woff') format('woff'), url('../fonts/GothamMedium.ttf') format('truetype'), url('../fonts/GothamMedium.svg') format('svg');
    	font-weight: normal;
    	font-style: normal;
    }
    @font-face {
        font-family: 'harabara_mais_bharabaramaisBd';
        src: url('../fonts/harabara_mais_bold-webfont.woff2') format('woff2'),
             url('../fonts/harabara_mais_bold-webfont.woff') format('woff');
        font-weight: normal;
        font-style: normal;
    }
    @font-face {
        font-family: 'adamnormal';
        src: url('../fonts/adam-webfont.woff2') format('woff2'),
             url('../fonts/adam-webfont.woff') format('woff');
        font-weight: normal;
        font-style: normal;
    
    }
    @font-face {
        font-family: 'silent_reactionregular';
        src: url('../fonts/silent_reaction-webfont.woff2') format('woff2'),
             url('../fonts/silent_reaction-webfont.woff') format('woff');
        font-weight: normal;
        font-style: normal;
    
    }
    *{font-size: 16px;}
    .padZero{padding-left: 0px}
    body .newNav{margin-bottom: 0px;background-color: #2196F3;border:none;}
    body .newNav .navbar-right li a,body .newNav .navbar-brand a{color: #fff}
    section{background-color: #EFF2F4}
    .leftSection{background-color: #2E3842;width: 80px;float:left;}
    .leftSection ul{list-style-type: none;text-align: center;padding: 20px 0}
    .midSection{background-color:grey;width: calc(100% - 450px);
    		float: left;color: #fff;padding-top: 15px;margin: 0 10px}
    .midSection ul li{display: inline-block;color: #fff}
    .midSection a,
    .midSection a:hover{color: #fff;}
    .textBlock{padding: 100px 0}
    .navigation li{padding: 10px 20px;text-transform:uppercase; }
    .heading{font-size: 80px;}
    .subHeading{font-size: 16px;max-width: 200px;width: 100%;margin: 0 auto;padding-bottom: 50px;}
    .textBlock p:last-child a,
    .textBlock p:last-child a:hover{text-decoration: underline;color: #fff}
    .border{border: 1px solid #fff;display: inline-block;border-radius: 50%;width: 40px;height: 40px;
    	text-align: center;padding-top: 7px;margin: 0 5px}
    .border a{color: #fff;border-radius: 50%;width: 40px;height: 40px;display: inline-block;}
    .rightSection{float: left;width: 100%;max-width: 350px;}
    .cross{color: #EDF0F2;display: inline-block;}
    .rightSection .nav-tabs li a{color: #8A9CAE}
    #home,#profile{padding: 15px;}
    #home ul{text-align: left}
    #home ul li{display: inline-block;}
    .circle {height:40px;width:40px;border-radius: 50%;display: inline-block;}
    .circle i{color: #fff} 
    #home ul li{position: relative;}
    #home ul li i{position: absolute;top: 10px;left: 10px}
    
    
    .uploads{text-align: center}
    .uploads li{display: inline-block;width: 32%;padding: 5px;}
    .uploads li img{width:100%;}

     

    I am here attaching the zip folder for the working demo:

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: