iLTC Home
Bookmark iLTC!
iLTC Google Boards

Google Groups
Receive The Code in Email!



Categories

Perl
  • Perl Tutorials Beginners

    Javascript
  • Javascript Tutorials
  • Javascript Tutorials Beginners

    HTML
  • HTML Tutorials



  • I love the code... dot com
    143,004

    iLoveTheCode.com
    Javascript > Javascript Tutorials > Javascript Change Fonts

    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


    Click HERE to comment or discuss at iLoveTheCode GOOGLE Group
    Previous Article: Javascript Alert
    Next Article: Javascript Change Font Size





    HTML & XHTMLHTML & XHTML

    Chuck Musciano, Bi...

    New $29.99

    HTML, XHTML, and CSS, Si...HTML, XHTML, and CSS, Sixth Edition ...

    Elizabeth Castro

    New $23.09

    CSSCSS

    David McFarland

    New $23.09

    Bulletproof Web DesignBulletproof Web Design

    Dan Cederholm

    New $23.99

    CSSCSS

    Eric Meyer

    New $29.69

    Head First HTML with CSS...Head First HTML with CSS & XHTML

    Eric Freeman, Elis...

    New $23.99

    jQuery in ActionjQuery in Action

    Bear Bibeault, Yeh...

    New $26.39

    New Perspectives on Crea...New Perspectives on Creating Web Pag...

    Patrick Carey

    New $69.26

    CSS MasteryCSS Mastery

    Andy Budd, Simon C...

    New $23.85


    Of Interest:
  • To Sort an Array in Perl
  • Making a Directory Using mkdir in Perl
  • Substitute in Perl
  • Renaming a File with Perl's RENAME Function
  • Javascript Superscript Text
  • Close Window with window.close in Javascript
  • Annoy Users with onBlur in Javascript
  • Substitute in Javascript Using replace Method
  • HTML Status
  • Make a New Paragraph Using HTML
  • HTML Title Tag
  • HTML Underline Text

  • More Articles:
  • Getting What is Matched in Perl
  • Exit a Loop with Last
  • Making a Directory Using mkdir in Perl
  • Matching at the Beginning and End
  • onClick Javascript
  • Javascript Alert
  • Exit a For Loop in Javascript with Break
  • Javascript Referrer
  • Javascript Reverse - Use Reverse on Items in an Array in Javascript
  • Get String Length with Javascript
  • HTML Font Code
  • Make a New Paragraph Using HTML
  • HTML Tags
  • HTML Superscript Text

  • Search iLTC w/ Google





    Recent Articles

  • Javascript Focus Form Field
  • Javascript Lowercase Text
  • Javascript Uppercase Text
  • Javascript Superscript Text
  • Javascript Subscript Text
  • Make Text Blink Using Javascript
  • Make a String into a Link in Javascript
  • Javascript Italics Text
  • Javascript Bold Text
  • Javascript Redirect
  • HTML Frames
  • HTML Color Chart
  • HTML Link
  • Javascript Status Bar








  • Perl Programming - Javascript Programming - Maybe Some PHP - Free Scripts - ILoveTheCode
    All Content © 2006 iLoveTheCode.com


    Contact Chris | About this Site


    439,769
    Counter by iLoveTheCode.com


    Last Modified: Friday, 23-Jun-2006 07:23:15 EDT