?
Solved

Linking 2 HTML checkboxes

Posted on 2014-04-14
8
Medium Priority
?
693 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
[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
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 2000 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
Industry Leaders: 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!

 

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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
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 …
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
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.
Suggested Courses

764 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