SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Change bodyfont onChange?

    I'm trying to figure out how to change the bodyfont with a selectfield and jQuery?

    Is it possible at all?

    Here is what I have so far:
    Code JavaScript:
    jQuery(function(jQuery)
    {
        jQuery(document).ready(function() {
        	jQuery("#mainfont").change(function() {jQuery(document.body).css("font-family",jQuery("#mainfont").getValue())});
        });
    });

    The php:
    PHP Code:
    echo 'Main Font: <select name="mainfont" id="mainfont">';
    $sql "SELECT * FROM ".$prefix."_cms_fonts ORDER BY id DESC";
    $result mysql_query($sql);
    while(
    $row mysql_fetch_array($result)){
        
        echo 
    '<option value="'.$row1['id'].'">'.$row1['font1'].'</option>';
        
    }
    echo 
    '</select>'
    In my db I have these 3 fileds (id, font1, font2)

    The id is important for when submitting/saving the form, font1 is a short description of the font, and font2 is the fontcode (ex. "Verdana, Geneva, sans-serif").

    Am I doing this all wrong, and can it be done?

  2. #2
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, got a bit closer but not there yet...
    Code HTML4Strict:
    <select name="font" onchange="document.body.style.fontFamily='courier'">
    <option value="courier">Courier</option>
    <option value="verdana">Verdana</option>
    </select>

    I need to get the option value up in the onChange command... Not sure how. Please help!

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by jmansa View Post
    Well, got a bit closer but not there yet...
    Code HTML4Strict:
    <select name="font" onchange="document.body.style.fontFamily='courier'">
    <option value="courier">Courier</option>
    <option value="verdana">Verdana</option>
    </select>

    I need to get the option value up in the onChange command... Not sure how. Please help!
    You can use the this keyword for that.

    onchange="document.body.style.fontFamily= this.value"
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •