Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 546721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:53:49+00:00 2026-05-13T10:53:49+00:00

I am making a colour-picker using pure JavaScript and HTML. It consists of three

  • 0

I am making a colour-picker using pure JavaScript and HTML. It consists of three HTML selects (drop downs boxes) and one div the background-colour of which will be changed by JavaScript. I am also trying to do this as “correctly” as possible. This means no Javascript code in the HTML.

My code so far looks like this:

    var red = document.getElementById('red');
    red.onchange = update();

    var green = document.getElementById('green');
    green.onchange = update();

    var blue = document.getElementById('blue');
    blue.onchange = update();

    var thebox = document.getElementById('colourbox');

    function d2h(d) {return d.toString(16);}
    function h2d(h) {return parseInt(h,16);} 

    function update(){
        finalcolor = '#' + d2h(red.value) + d2h(green.value) + d2h(blue.value)
        thebox.style.background = finalcolour;
    }

And the HTML looks like this:

<div id="colourbox"></div>
<form name="myform" action="colour.html">
    <select name="red" id="red">
        <option value="0">0</option>
        .... etc etc ....
    </select>
    <select name="green" id="red">
        <option value="0">0</option>
        .... etc etc ....
    </select>
    <select name="blue" id="red">
        <option value="0">0</option>
        .... etc etc ....
    </select>
</form>

The trouble is that all the document.getElementById() calls return null. Presumably because they don’t exist at the time that the code is run. I have tried putting the code inside an window.onload = function() {} but a) that just gets confusing and b) I would then have to define the update function inside a function, which doesn’t seem right.

Can anyone shed some light on this? Are there any general rules which might help me understand how it works? Or some documentation on the subject?

EDIT: revised code:

<script type="text/javascript">
    window.onload = function(){
        var red = document.getElementById('red');
        red.onchange = update();

        var green = document.getElementById('green');
        green.onchange = update();

        var blue = document.getElementById('blue');
        blue.onchange = update();

        var thebox = document.getElementById('colourbox');

        function d2h(d) {return d.toString(16);}
        function h2d(h) {return parseInt(h,16);} 

        function update(){
            var finalcolor = '#' + d2h(document.getElementById('red').value) + d2h(document.getElementById('green').value) + d2h(document.getElementById('blue').value);

            document.getElementById('colourbox').style.background = finalcolor;
        }

    }
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:53:50+00:00Added an answer on May 13, 2026 at 10:53 am

    putting code in a handler for the onload event works just fine, and is widely accepted. So is putting a function within a function (depending on the reason).

    var mypage = {
        red: null,
        green: null,
        blue: null,
        colourbox: null,
    
        d2h: function(d) {
            var hex = d.toString(16);
            if (hex.length < 2) {
                hex = '0' + hex;
            }
            return hex.toUpperCase();
        },
    
        h2d: function(d) {
            return parseInt(h, 16);
        },
    
        update: function() {
            mypage.colourbox.style.backgroundColor = '#' + mypage.d2h(mypage.red.value) + mypage.d2h(mypage.green.value) + mypage.d2h(mypage.blue.value);
        }
    };
    
    window.onload = function() {
        mypage.red = document.getElementById('red');
        mypage.red.onchange = mypage.update;
    
        mypage.green = document.getElementById('green');
        mypage.green.onchange = mypage.update;
    
        mypage.blue = document.getElementById('blue');
        mypage.blue.onchange = mypage.update;
    
        mypage.colourbox = document.getElementById('colourbox');
    }
    

    Here is a blog post by dean edwards about using window.onload

    Another option is to put your javascript at the bottom of the page instead of at the top.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When making changes using SubmitChanges() , LINQ sometimes dies with a ChangeConflictException exception with
I've been making a concerted effort to improve my javascript skills lately by reading
I've got a custom colour set in Visual Studio and one of the colours
I am making a browser based javascript game. As such, within the game there
Im making an app for the iphone using cocos2d and i am trying to
I am making a Color class, and provide a standard constructor like Color(int red,
Making echo of a question around the web: Is the syntax for svn:ignore patterns
After making some changes in my models (eg. new field in a model and
After making a few modifications to a rails app I am tinkering on, railroad
Im making a small python script to upload files on the net. The script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.