Home > ASP.Net, Javascript > Change Font Dynamically using Javascript

Change Font Dynamically using Javascript


Hi

you can check this previous post

Dynamic font size change using javascript

now i’m going to provide second way to change font dynamically using Javascript

Javascript Function :-

<script type="text/javascript">
    //Specify affected tags. Add or remove from list:
        var tgs = new Array( 'div','td','tr', 'p', 'span', 'font');
        //Specify spectrum of different font sizes:
        var szs = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
        var startSz = 2;
        function ts( trgt,inc )
        {
         if (!document.getElementById) return
         var d = document,cEl = null,sz = startSz,i,j,cTags;
         sz += inc;
         if ( sz < 0 ) sz = 0;
         if ( sz > 6 ) sz = 6;
         startSz = sz;
         if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];
         cEl.style.fontSize = szs[ sz ];
         for ( i = 0 ; i < tgs.length ; i++ ) {
          cTags = cEl.getElementsByTagName( tgs[ i ] );
          for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = szs[ sz ];
         }
        }
    </script>

HTML Page :-

 <div style =" text-align:left; width:100%; padding:0; margin:0">
      <a href="javascript:ts('main',1)" ><img src="zoom-in.gif" alt="Zoom In" /></a> |
      <a href="javascript:ts('main',-1)"><img src="zoom-out.gif"  alt="Zoom Out" /></a>
   </div>
   <div id="dvKeys" style="display:none">
   </div>
   <div id="main">
    any text here
</div>

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: