Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Linking 2 HTML checkboxes

Posted on 2014-04-14
8
Medium Priority
?
743 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Technology Partners: 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!

Question has a verified solution.

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

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.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Suggested Courses

636 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