Solved

Linking 2 HTML checkboxes

Posted on 2014-04-14
8
548 Views
Last Modified: 2014-04-22
I have two checkboxes in seperate basic html tables.  I'm trying to get an easy javascript if statement to link the two together. So if checkbox1 is checked, so is checkbox2.
Thanks!


<table class="table1">
<tr>
                            <td>
                                <input type='checkbox' class='list_item' name="checkbox1" id=2 value=2 onclick='updateLayerVisibility();' />
                            </td>
                           
                        </tr>
</table>
<table class="table2">
                        <tr>
                            <td>
                                <input type='checkbox' class='list_item' name="checkbox2" id='2' value=2 onclick='updateLayerVisibility();' />
                            </td>
                           
                        </tr>
                       
</table>

Open in new window

0
Comment
Question by:Gregory3
8 Comments
 
LVL 26

Expert Comment

by:EddieShipman
ID: 40012820
First, you need distinct id values. The first one can't be id=2 because it must be enclosed in quotes.

Once you get that squared away, you can do something this in your function:
(document.getElementById("2").checked ? document.getElementById("2").checked = document.getElementById("1").checked : document.getElementById("1").checked = document.getElementById("2").checked);

Open in new window

0
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 40012983
As EddieShippman has stated, you need distinct id attributes. So this example changes both the id and onclick attributes.
<table class="table1">
	<tr>
		<td>
			<input type='checkbox' class='list_item' name="checkbox1" id="cb1" value=2 onclick='updateLayerVisibility(); document.getElementById("cb2").checked = this.checked;' />
		</td>
	</tr>
</table>
<table class="table2">
	<tr>
		<td>
			<input type='checkbox' class='list_item' name="checkbox2" id="cb2" value=2 onclick='updateLayerVisibility(); document.getElementById("cb1").checked = this.checked;' />
		</td>
	   
	</tr>
</table>

Open in new window

It's worth noting that the HTML 4.01 specification requires the id and name attributes to begin with a letter. Although several references state this to be true for XHTML, too, I can't find it in the XHTML specification. This restriction has been eliminated in the proposed specification for HTML5.
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 40014221
if (document.getElementsByName("checkbox1")[0].checked) {
document.getElementsByName("checkbox2")[0].checked=true;
}
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Gregory3
ID: 40016238
Thanks for your responses.  
I have a variant of Sar1973's example working, but that is only for checking them both. The trick is getting them both checked and unchecked by clicking either one.  Hope that makes sense.  

EddieShipman I like your ternary  operator example but I can't get it to jive with my function.
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 40016246
Did you try my example?
0
 

Author Comment

by:Gregory3
ID: 40016257
Sorry about that, I missed that one.  That worked!  Much appreciated.
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 40016272
You're welcome. Glad it worked.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

856 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