473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

grab cell (<TD>) bgcolor with onClick and put it in a input type text...

Hi,

I have the following simple code:

<TABLE><TR>
<TD BGCOLOR="#FFFFFF" ONCLICK="myform.color.value =
this.bgcolor">hello</TD>
</TR></TABLE>

<FORM ACTION="hello.cgi" NAME="myform" ID="myform">
<INPUT TYPE="text" NAME="color" SIZE="10" MAXLENGTH="10">
</FORM>

When I click on the cell, the text input box shows 'undefined'. How
can I insert the cell's bgcolor hex code (#FFFFFF) into the form's
box.

Thanks,
A.S.
Jul 20 '05 #1
4 6617

"A.S." <an**@cyber-wizard.com> wrote in message
Hi,

I have the following simple code:

<TABLE><TR>
<TD BGCOLOR="#FFFFFF" ONCLICK="myform.color.value =
this.bgcolor">hello</TD>
</TR></TABLE>
<table><tr>
<td style="background-color:#ffffff"
onclick="document.myform.color.value =
this.style.backgroundColor">hello</td>
</tr></table>
<FORM ACTION="hello.cgi" NAME="myform" ID="myform">
<INPUT TYPE="text" NAME="color" SIZE="10" MAXLENGTH="10">
</FORM>

When I click on the cell, the text input box shows 'undefined'. How
can I insert the cell's bgcolor hex code (#FFFFFF) into the form's
box.

Thanks,
A.S.


Hope this helps

Geoff
Jul 20 '05 #2
"A.S." <an**@cyber-wizard.com> wrote in message
news:b1**************************@posting.google.c om...
<snip>
<TABLE><TR>
<TD BGCOLOR="#FFFFFF" ONCLICK="myform.color.value =
this.bgcolor">hello</TD>
</TR></TABLE>

<snip>

Whatever else you do and whatever your motivation for wanting to do
this, JavaScript is a case sensitive language and the correct name for
the property that you are reading is - bgColor - (upper case "C" at the
start of "Color").

However - myform.color.value - is an (almost) IE only property accessor
as named/IDed forms are not made available as properties of the global
object on many browsers including Mozilla, Netscape 6+ and all other
browsers in the Gecko-based family. It would be better to use global
form references that are relative to the document object and preferably
the W3C DOM Level 2 specified (and extremely back-compatible)
document.forms collection.

Richard.
Jul 20 '05 #3
"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message news:<bg*******************@news.demon.co.uk>...
"A.S." <an**@cyber-wizard.com> wrote in message
news:b1**************************@posting.google.c om...
<snip>
<TABLE><TR>
<TD BGCOLOR="#FFFFFF" ONCLICK="myform.color.value =
this.bgcolor">hello</TD>
</TR></TABLE>

<snip>

Whatever else you do and whatever your motivation for wanting to do
this, JavaScript is a case sensitive language and the correct name for
the property that you are reading is - bgColor - (upper case "C" at the
start of "Color").

However - myform.color.value - is an (almost) IE only property accessor
as named/IDed forms are not made available as properties of the global
object on many browsers including Mozilla, Netscape 6+ and all other
browsers in the Gecko-based family. It would be better to use global
form references that are relative to the document object and preferably
the W3C DOM Level 2 specified (and extremely back-compatible)
document.forms collection.

Richard.


Richard, I completely agree with you. I need the script to be
back-compatible as much as possible. What would you suggest I should
use in my javascript to accomplish this? The script suggested above
uses styles, which are very recent, too.
Jul 20 '05 #4
"A.S." <an**@cyber-wizard.com> wrote in message
news:b1**************************@posting.google.c om...
<snip>
<TABLE><TR>
<TD BGCOLOR="#FFFFFF"
ONCLICK="myform.color.value =this.bgcolor">hello</TD>
</TR></TABLE>
<snip>... . It would be better to use global
form references that are relative to the document object and
preferably the W3C DOM Level 2 specified (and extremely
back-compatible) document.forms collection.
Richard, I completely agree with you. I need the script to be
back-compatible as much as possible. What would you suggest
I should use in my javascript to accomplish this? The script
suggested above uses styles, which are very recent, too.


As I global reference to the form element's value I would always use:-

document.forms["myform"].elements["color"].value
- or -
document.forms.myform.elements.color.value

- as it will work in every JavaScript capable browser that I am aware of
including the oldest still in use.

As far as the style objects are concerned, they have been around since
IE 4 and I am not aware of a subsequent JavaScript capable browser that
does not implement them. The browser that would cause most trouble with
a script that attempted to use the style object of elements would be
Netscape 4, but Netscape 4 does not implement onclick events on TD
elements so your code will never be executed in that environment anyway.

On the other hand the bgcolor attribute has been deprecated on TD
elements (and others) so its use might have forward compatibility
problems (though the - bgColor - property is still present in the DOM
level 2 specification).

However, you have not said what it is that you are trying to do with
this script. There might be alternatives that would better achieve your
goal.

Richard.
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: El Diablo | last post by:
Hi there, I'm trying dynamically generate extra rows in a table. So far this achieves this task within the tHead segment: theTable.tHead.appendChild(document.createElement('TR')) ....but...
2
by: trusst_3 | last post by:
Hello, This is the only line in my entire code that does not validate in XHTML : <td width="24" bgcolor="#330066" onclick="javascript:color1()" class="borderWidthr"></td> However, it is...
2
by: js | last post by:
I have a table rendered with XSLT. The first column has a radio button controls for user to make a selection for a particular row. All the values in the remaining columns are all concated with a...
2
by: Pete Kipe | last post by:
I'm not a JavaScript programmer...but I'm trying to put together a simple menu system for a new website and need a little help. I have the following script: <SCRIPT language=javascript> <!--...
5
by: Derek Fountain | last post by:
I have a horizontal navigation bar, which is a single row table containing the right images. It "stretches" itself across the screen using a penultimate td like this: <td width="100%"...
3
by: RC | last post by:
I have a very sime html table like <html><head><title>My Table</title> <style> input { margin: 0; padding: 0; border-width: 0; text-indet: 0; text-align: left } </style></head><body> <table...
2
by: leena | last post by:
My code is this <td bgcolor="#EBEBD8" onMouseOver="bgColor='#CBCBB8'"onMouseOut="bgColor='#EBEBD8'"> i want to put same hover colour on onclick function so how i write that code. If anybody know...
5
by: Michael | last post by:
Hi. I need dinamically calculate input text field based on parent static TD before showing content of input. Please, advice. Michael
4
by: John | last post by:
I am attempting to make a table data row editable once clicked on but I am not sure how to show the <tdwith two different states. I've got it to the point where the <tdis hidden then appears within...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.