Solved

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

Posted on 2008-06-26
4
222 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

777 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