tags:

views:

42

answers:

5

Can someone show me whats wrong with this:

<html>

    <script type = "text/javascript">
        function colourGreen()
        {
            document.getElementById("button1").style.bgColor = 0xFFFF00;
        }
    </script>
    <body>

        <form action="">
            <div id = "button1">
                <input type="button" value="Colour">
            </div id>
            <div id = "button2">
                <input type="button" value="Price up" onclick = "colourGreen()">
            </div id>
            <div id = "button3">
                <input type="button" value="Price down">
            </div id>
        </form>

    </body>
</html>
+2  A: 
document.getElementById("button1").style.backgroundColor = '#FFFF00';
Darin Dimitrov
#FFFF00 will get yellow. Try #00FF00 for green.
DOK
+1  A: 

Try:

document.getElementById("button1").style.backgroundColor = '#00ff00';

It is backgroundColor not bgColor

Sarfraz
Thank you very much! i remember you helped me before :)Also to anyone else reading, don't use a div as that colours the whole line and not the button!
Tom
@Tom, if you find this answer helpful you should accept it (check the green tick next to the answer). 0% accept rate is really bad for your reputation. You should reward people's time trying to help you. It's the least you can do.
Darin Dimitrov
#FF0000 will get you red. Try #00FF00 for green.
DOK
A: 

I'd try

.style.backgroundColor = 0xFFFF00;
Larry K
Unfortunately this won't work. `0xFFFF00` in invalid value.
Darin Dimitrov
Oops; thanks. I had focused on the OP's incorrect style attribute.
Larry K
A: 

I assume your divs are only as big as your buttons and are therefore hidden by the buttons themselves. Change the colour on you button itself instead of the div?

A neater and more reliable way to to edit css of items is using jquery selectors. $("#button1").css("background-color","#ff0000"); Be sure to include the jquery .js file before trying this or you'll get an object expected error.

Byron Cobb
A: 

You can create a css rule and change the className of the button to link to that rule.

<style type="text/css">
     .buttonColor{
          background-color: green;
     }
</style>

<script type ="text/javascript">
    function colourGreen() {
        document.getElementById("button1").className = "buttonColor";
    }
</script>

That way if you for some reason decide to change the color or the background you will not have to change it on every page. You will be able to change that one css file.

John