Solved

Linking 2 HTML checkboxes

Posted on 2014-04-14
8
594 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 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

739 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