Solved

Linking 2 HTML checkboxes

Posted on 2014-04-14
8
472 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
Comment Utility
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 21

Accepted Solution

by:
Kim Walker earned 500 total points
Comment Utility
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
Comment Utility
if (document.getElementsByName("checkbox1")[0].checked) {
document.getElementsByName("checkbox2")[0].checked=true;
}
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Gregory3
Comment Utility
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 21

Expert Comment

by:Kim Walker
Comment Utility
Did you try my example?
0
 

Author Comment

by:Gregory3
Comment Utility
Sorry about that, I missed that one.  That worked!  Much appreciated.
0
 
LVL 21

Expert Comment

by:Kim Walker
Comment Utility
You're welcome. Glad it worked.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

762 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

9 Experts available now in Live!

Get 1:1 Help Now