iLTC Home
Bookmark iLTC!

Categories

Perl
  • Perl Tutorials Beginners

  • Javascript
  • Javascript Tutorials
  • Javascript Tutorials Beginners

  • HTML
  • HTML Tutorials

  • Command Line
  • Command Line Tutorials

  • How Tos
  • Other Tips and Tricks



  • for(var i;i < 1000;i++) { }
    2210928

    iLoveTheCode.com

    Javascript Change Fonts

    We'll start by using Javascript to change fonts on the entire page. To do this, we'll create a body tag and two links.

    NOTE: If you copy and paste this code, watch out for spaces and line returns put in by the browser, like after the getElementById.


    <body id=hey>

    <a onMouseover=document.getElementById('hey').style.fontFamily='courier'> Make me COURIER!</a> ----
    <a onMouseover=document.getElementById('hey').style.fontFamily='verdana'> Make me VERDANA!!!</a>


    -In our HTML body tag, we name our id attribute hey because we like the name.
    -We create two links that use the Javascript onMouseover event handler.
    ---After the equals sign:
    -----document - this page
    -----getElementById - Javascript's built-in method that will get elements from our page.
    -----In the parenthesis - we state that we will be working on the hey element of our page.
    -----We then call the Javascript style object.
    -----We then call the fontFamily property of the style object.
    -----We say make it courier.


    NOTES: We used single quotes, but we could have used double since we lazily didn't enclose our entire onMouseover in double quotes. Had we done that, the single quotes would have been required.


    THE GREAT PART. We can use this same bit of code, with minor tweeks, to change all kinds of things on our page. Like background colors:

    <body id=hey>
    <a onMouseover=document.getElementById('hey').style.backgroundColor='red'> Make me Red!</a> ----
    <a onMouseover=document.getElementById('hey').style.backgroundColor='green'> Make me GREEN!!!</a>

    -We just changed the style object to backgroundColor and we have a blinding background changer.


    We can change paragraphs of text:

    <p id=hey>This is my fabulous text that will change when you mouseover the stuff below.</p>
    <a onMouseover=document.getElementById('hey').style.fontFamily='courier'> Make it COURIER!</a> ----
    <a onMouseover=document.getElementById('hey').style.fontFamily='verdana'> Make it VERDANA!!!</a>


    Even change tables!... Suddenly I feel like that guy selling Oxy-Cleaner or some such thing... but it does really really work!

    <table id=hey border=1><tr><td>
    <a onMouseover=document.getElementById('hey').style.backgroundColor='red'> Make me Red!</a> ----
    <a onMouseover=document.getElementById('hey').style.backgroundColor='green'> Make me GREEN!!!</a>
    </td></tr></table>


    Of course we can use this with all kinds of event handlers as well as in functions we write. Here we'll use the onClick event handler:

    <p id=hey>This is my fabulous text that will change when you CLICK the stuff below.</p>
    <a onClick=document.getElementById('hey').style.fontFamily='courier'> Make it COURIER!</a> ----
    <a onClick=document.getElementById('hey').style.fontFamily='verdana'> Make it VERDANA!!!</a>


    One more for fun. Change font size:

    <p id=hey>This is my fabulous text that will change when you CLICK the stuff below.</p>
    <a onClick=document.getElementById('hey').style.fontSize='10'> Make it Tiny!</a> ----
    <a onClick=document.getElementById('hey').style.fontSize='24'> Make it HUGE!!!</a>


    There's a link here and below to a long list of style object properties that will give you even more power to bring all your web pages to life.



    Resources:
    irt.org - Style Object Properties
    Javascript onMouseover
    onClick Javascript
    Javascript Change Background Colors
    Javascript Change Font Size




    Previous Article: Javascript Alert
    Next Article: Javascript Change Font Size






    Search iLTC w/ Google




    Recent Articles

  • HTML Font Code
  • Javascript and CSS - Easy Pop Up Help Balloons
  • Use OnMouseOver in Javascript
  • Use onMouseOut in Javascript
  • Use innerHTML to Change Text on a Page
  • Renaming a File with Perl's RENAME Function
  • PRINT to Browser with document.write Command in Javascript
  • Passing an Argument or Variable to a Javascript
  • Opening an Alert Box in Javascript
  • Opening a REMOTE WINDOW in Javascript
  • OPEN and WRITE a Data File in Perl
  • OPEN and READ a Data File in Perl
  • Mimic Typing from a Textfield in a Textarea with Javascript
  • Javascript Reset Form








  • Perl Programming - Javascript Programming - HTML Code - Mac Unix Command Line - Maybe Some PHP - Free Scripts - ILoveTheCode
    All Content © 2006-2009 iLoveTheCode.com


    Contact Chris | About this Site


    2210139
    Counter by iLoveTheCode.com


    Last Modified: Saturday, 10-Jan-2009 05:40:24 EST



    Hosted as well :)
    Home-brewed logo - 1997