/    Sign up×
Community /Pin to ProfileBookmark

change head definition

hey
im new to javascript and im trying to make a document in html
that will have a button that changes definition of the document (for example a button that changes b.g color)without loading a new page and i need the button to repeat the action with every time it is clicked to a random color.
can some one please help!!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliJan 31.2005 — you need to post your questionin the JavaScript forums first.

I'll move this thread to that..
Copy linkTweet thisAlerts:
@CharlesJan 31.2005 — Aack! That's a terrible example.

See http://www.alistapart.com/articles/alternate/ .
Copy linkTweet thisAlerts:
@A1ien51Jan 31.2005 — [i]Originally posted by Charles [/i]

[B]Aack! That's a terrible example.



See http://www.alistapart.com/articles/alternate/ . [/B]
[/QUOTE]


Your solution you just posted would mean you would need to create 1000's of style sheets to get a wide range of random colors. Hence loading other files.

I know that the hex conversion could be a lot better!

Eric
Copy linkTweet thisAlerts:
@CharlesJan 31.2005 — [i]Originally posted by A1ien51 [/i]

[B]Your solution you just posted would mean you would need to create 1000's of style sheets to get a wide range of random colors. Hence loading other files.



Eric [/B]
[/QUOTE]
True, but we don't know how many different background colors we're talking about or even if we're talking about the whole document.

  • * For the whole document we could use a nice valid DOM level 0 method:


  • [font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

    "http://www.w3.org/TR/html4/strict.dtd">

    <html lang="en">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <meta name="Content-Script-Type" content="text/javascript">

    <meta name="Content-Style-Type" content="text/css">

    <title>Example</title>

    </head>

    <body>

    <script type="text/javascript">

    <!--

    function randomDigit () {

    n = Math.ceil (216 * Math.random()).toString(16);

    return /../.test(n) ? n : '0' + n;

    }

    function randomColor () {return randomDigit () + randomDigit () + randomDigit ()}

    document.write('<button onclick="document.bgColor = randomColor()">BG Color</button>');

    // -->

    </script>

    </body>

    </html>[/font]

  • * The method I described above shows the use of the DOM level 1.


  • * And if we wat to style the background of some element, we could use DOM level 2:


  • [font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

    "http://www.w3.org/TR/html4/strict.dtd">

    <html lang="en">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <meta name="Content-Script-Type" content="text/javascript">

    <meta name="Content-Style-Type" content="text/css">

    <title>Example</title>

    </head>

    <body>

    <p id="lorem">Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum.</p>

    <script type="text/javascript">

    <!--

    function randomDigit () {

    n = Math.ceil (216 * Math.random()).toString(16);

    return /../.test(n) ? n : '0' + n;

    }

    function randomColor () {return '#' + randomDigit () + randomDigit () + randomDigit ()}

    function change () {document.getElementById ('lorem').style.backgroundColor = randomColor()}

    document.write('<button onclick="change()">BG Color</button>');

    // -->

    </script>

    </body>

    </html>[/font]
    ×

    Success!

    Help @savonge spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.6,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...