Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 26th, 2004, 07:10 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default Button Colours

Hi guys,

Anyone know how to change the background-color attribute of all the buttons in a page at once?

cheers
interrupt

__________________
\'sync\' <cr>
The name specified is not recognized as an internal or external command, operable program or batch file.
 
Old August 26th, 2004, 07:26 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well there are two types of buttons, INPUTs with a type equal to button and BUTTONs. The code would be:
Code:
function changeButtonBackground(Colour)
{
  var colInputs = document.getElementsByTagName("input");
  for (var i = 0; i < colInputs.length; i++)
  {
    var oInput = colInputs[i];
    if (oInput.type.toLowerCase() == "button")
    {
      oInput.style.backgroundColor = Colour;
    }
  }
  var colButtons = document.getElementsByTagName("button");
  for (var i = 0; i < colButtons.length; i++)
  {
    colButtons[i].style.backgroundColor = Colour;
  }
}
Then somewhere you'd call
Code:
changeButtonBackground("#dd0000");
--

Joe
 
Old August 26th, 2004, 07:33 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Joe, thats great. Is there a way to write a prompt into that? I want the user to pick their own button colours.....sorry to be a pain!

interrupt

 
Old August 26th, 2004, 08:16 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

How flash do you want it? With IE6 you can use special box, or for older browsers a prompt dialog:

Code:
<html>
<head>
<title>Colour Picker</title>
<script type="text/javascript">

function changeButtonBackground(Colour)
{
  var colInputs = document.getElementsByTagName("input");
  for (var i = 0; i < colInputs.length; i++)
  {
    var oInput = colInputs[i];
    if (oInput.type.toLowerCase() == "button")
    {
      oInput.style.backgroundColor = Colour;
    }
  }
  var colButtons = document.getElementsByTagName("button");
  for (var i = 0; i < colButtons.length; i++)
  {
    colButtons[i].style.backgroundColor = Colour;
  }
}


function getColourFromDialog(InitialColour)
{
    var sInitial = InitialColour;
  var sColour = dlgHelper.ChooseColorDlg(sInitial);    
    sColour = sColour.toString(16);
  var iLength = sColour.length;
    sColour = "000000" + sColour;
    sColour = sColour.substr(iLength);
    return "#" + sColour;
}

function  changeColoursWithPicker(CurrentColour)
{
  var sInitial = CurrentColour;
  var sColour = getColourFromDialog(sInitial);
  changeButtonBackground(sColour);
}

function changeColoursWithPrompt()
{
  var sColour = prompt("Enter a colour.", "#00dd00");
  try
  {
    changeButtonBackground(sColour);
  }
  catch(e)
  {
    alert(e.message);
  }
}
</script>
</head>
<body>
<input type="button" value="Change with colour picker" onclick="changeColoursWithPicker(this.currentStyle.backgroundColor);"><br><br>
<input type="button" value="What's my colour?" onclick="alert(this.currentStyle.backgroundColor);"><br><br>
<button onclick="changeColoursWithPrompt();">Change with prompt</button>

<OBJECT id="dlgHelper" CLASSID="clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b" width="0px" height="0px" VIEWASTEXT></OBJECT>
</body>
</html>
--

Joe
 
Old August 26th, 2004, 12:55 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

If you want to change the appearance at design time, you can also use CSS:

<style type="text/css">

button, input
{
 background-color: red;
}
</style>

This code will change the appearance of all input and button elements. To avoid drop-down and other controls from changing as well, you could create a separate class for the buttons.

Cheers,

Imar
 
Old August 26th, 2004, 01:11 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Imar,

I believe Interrupt would like a prompt like Joe's solution.

Also, FWIW, instead of inventing a class to do the job of not changing non-button inputs, I think it's easier to use a CSS2/CSS3 selector like so:

button, input[type=button], input[type=submit]
{
 background-color: red;
}

HTH,

Snib

<><
 
Old August 26th, 2004, 04:51 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Quote:
quote:Originally posted by Snib

Also, FWIW, instead of inventing a class to do the job of not changing non-button inputs, I think it's easier to use a CSS2/CSS3 selector like so:

button, input[type=button], input[type=submit]
{
background-color: red;
}
If you can find a browser that supports it :)

--

Joe (Co-author Beginning XML, 3rd edition)
 
Old August 26th, 2004, 04:57 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Actually, I use this...

http://dean.edwards.name/IE7/intro

...and it fixes IE5-6 up like Firefox ;)

HTH,

Snib

<><





Similar Threads
Thread Thread Starter Forum Replies Last Post
List Box Colours Brendan Bartley Access 1 September 26th, 2007 10:16 AM
Need suggestion to revert colours in menu vighna XSLT 3 August 31st, 2007 03:56 PM
system colours DarrenMelling Pro VB 6 3 December 7th, 2006 06:22 AM
alternating colours ozPATT Excel VBA 3 October 5th, 2005 10:19 AM
how to highlight in three colours? have code but gilgalbiblewheel Classic ASP Databases 3 October 2nd, 2004 11:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.