views:

197

answers:

3

hi everyone, can anyone pls give sugessions on how to write the script for selecting a color in a live page so that the selected color apply to whole page.can any one pls provide me a sample scripts for dis.

A: 

If by "applying the color to the whole page" you are talking about changing the background color of the page, try something like:

<input type="text" id="col" value="Enter hex color here" />
<input type="button" value="Change" onclick="changeColor()"/>
<script>
function changeColor()
{
  col = document.getElementById("col").value;
  /*
    Make sure the col is indeed a valid one.
    Acceptable formats include:
    #789abc
    #xyz //expanded to #xxyyzz
    rgb(0, 127, 255)
    Standard colors like "red", "blue" etc.
  */
  document.body.style.backgroundColor = col;
}
</script>

To avoid errors caused by ignorant user inputs, you can use a drop-down-box instead of the text-field and populate it with allowed values.

Amarghosh
the code is ve ng errors...??
Patel
@patel pardon - what kinda error?
Amarghosh
+1  A: 

Try using a jQuery Colorpicker like this one. I'm guessing that's what you mean by 'selecting'.

Rowno
+1  A: 

if you want to change the background color of your page dynamically with PHP here are the tips to start:

  • create a simple PHP page like this (ugly but fast solution):

    <?php
    function getBackgroundColor() {
      switch(rand(0, 3)) {
        case 1:
          return "#f00";
        case 2:
          return "#0f0";
        case 3:
          return "#00f";
        default:
          return "#fff";
      }
    }
    ?>
    <html>
    <head><title>test page</title></head>
    <body style="background-color: <?php echo getBackgroundColor(); ?>">
    here comes the body
    </body>
    </html>
    
  • you can create a template file for the PHP and do the same just put the HTML in a different file

  • do with AJAX the PHP will answer only the color and your javascript will change the background color
KARASZI István
hi ur code works fine but. when i refresh the page the color is changing but when i tried to change the color on button click its nt working any suggesions
Patel
my code uses random colors for every reload (white, red, green, blue)if you want to change the color by clicking you should rewrite the getBackgroundColor() to do the work
KARASZI István