[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get Id of element

Posted on 2007-10-21
3
Medium Priority
?
6,778 Views
Last Modified: 2008-01-09
Hello, how can I get the id of an element and place the value into a textbox?  Heres what I have:

<td  id="#CCFFCC" style="background-color:#CCFFCC" width="20" height="20" onclick="javascript:toggleLayer( this,'TextBox1', 'TBL');"></td>

The javascript:
function toggleLayer( Colorcntrl ,txtboxcntrl, TBL, HexColor)
{
    document.getElementById(txtboxcntrl).value = Colorcntrl.?????????????????????;
 }

Thanks
0
Comment
Question by:grogo21
  • 2
3 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 20118641
The syntax should be:
document.getElementById(txtboxcntrl).value = Colorcntrl.id
But, I believe the id property must start with a alphabetic character.
This I would remove the "#" from the Id name and change the statement to
document.getElementById(txtboxcntrl).value = "#"+Colorcntrl.id
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 20118659
Here it is complete:
<td  id="CCFFCC" style="background-color:#CCFFCC" width="20" height="20" onclick="javascript:toggleLayer( this,'TextBox1', 'TBL');"></td>

The javascript:
function toggleLayer( Colorcntrl ,txtboxcntrl, TBL, HexColor)
{
    document.getElementById(txtboxcntrl).value = "#"+Colorcntrl.id;
 }


But perhaps better:
<td  id="MyElement" style="background-color:#CCFFCC" width="20" height="20" onclick="javascript:toggleLayer( this,'TextBox1', 'TBL');"></td>

The javascript:
function toggleLayer( Colorcntrl ,txtboxcntrl, TBL, HexColor)
{
    document.getElementById(txtboxcntrl).value = Colorcntrl.style.backgroundColor;
 }

And yet another way of doing it
<td  id="MyElement" style="background-color:#CCFFCC" width="20" height="20" onclick="javascript:toggleLayer( this.style.backgroundColor,'TextBox1', 'TBL');"></td>

The javascript:
function toggleLayer( Colorcntrl ,txtboxcntrl, TBL, HexColor)
{
    document.getElementById(txtboxcntrl).value = Colorcntrl;
 }

0
 

Author Comment

by:grogo21
ID: 20118753
Thanks, This one works:
 document.getElementById(txtboxcntrl).value = "#"+Colorcntrl.id;

Using  document.getElementById(txtboxcntrl).value = Colorcntrl.style.backgroundColor;
changes the hex color into rgb in Firefox btw.

Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question