By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,287 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

changing the background colour of a element in a page

P: n/a
I am trying to change the background colour of either a div or a textarea
with javascript, but am having problems.

I have passed a hex colour value in the variable hval, and with the
following code can change the background colour of the whole page, but only
(now) want to change either a textarea of a form or a div.

I'm stuck.

document.bgColor = hval;

I'm wondering if it is something to do with style.backgroundColor, but am
not sure how to implement it...

John

Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
John D wrote:
I am trying to change the background colour of either a div or a textarea
with javascript, but am having problems.

I have passed a hex colour value in the variable hval, and with the
following code can change the background colour of the whole page, but only
(now) want to change either a textarea of a form or a div.

I'm stuck.

document.bgColor = hval;

I'm wondering if it is something to do with style.backgroundColor, but am
not sure how to implement it...

John


I'm not sure what you mean by the "change either a textarea of a form or a
div". <textarea> is a form element, you wouldn't embed it in a DIV. Presumably
you mean you want to simply change the background color of a <form> element or
<div> element? If that's the case:

<form id="myFormId"> or <div id="myDivId">

<a href="#"
onclick="changeBgColor('myDivId', '#ff0000');return false;">Test link</a>

function changeBgColor(id, color) {
var item;
if (document.getElementById) {
item = document.getElementById(id);
} else if (document.all) {
item = document.all(id);
}

if (item && item.style) {
item.style.backgroundColor = color;
}
}

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.