Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-26
4
Medium Priority
?
240 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

916 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