Solved

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

Posted on 2008-06-26
4
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Find out what you should include to make the best professional email signature for your organization.
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…
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 …

756 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