Solved

How do I get a reference to the label of a checkbox?

Posted on 2008-06-26
4
221 Views
Last Modified: 2010-04-21
I have the following HTML code in my page:

<td><input type="checkbox" id="mycheckbox"><label for="mycheckbox">This is my checkbox</label></td>

What I need to do is use javascript to show and hide the checkbox and associated label when another action happens on the page.  I have the code working for doing the checkbox, but I don't know how to get a reference to the label to make it hide.

What is the javascript to get a reference to the label?  Alternatively, I could set the td to hide, if I could get a reference to that.  I cannot change the HTML code (no adding id, name, or class properties).
0
Comment
Question by:SkipFire
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
Rob Siklos earned 500 total points
ID: 21876914
Can you get a reference to the <td> using:

   var parentNode = document.getElementById("mycheckbox").parentNode;

and then apply "style= display: none" to all the child elements of the td
0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 21876998
You can use the id property.

<td><input type="checkbox" id="mycheckbox"><label id="mylabel" for="mycheckbox">This is my checkbox</label></td>

document.getElementById("mylabel").style.display='none'
0
 
LVL 4

Author Comment

by:SkipFire
ID: 21877226
cyberwebservice, I said I cannot change the HTML provided and the HTML provided does not have an ID property on the label.

rob263, it worked, thanks.
0
 
LVL 4

Author Closing Comment

by:SkipFire
ID: 31471075
The code to execute and style settings once I had the object was a bit off, but getting the object was dead on and getting the object was all I needed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article discusses how to create an extensible mechanism for linked drop downs.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now