Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn how to dynamically set the form action using jQuery.

758 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

25 Experts available now in Live!

Get 1:1 Help Now