Solved

Check a checkbox and then automatically other checkbox

Posted on 2008-10-28
9
278 Views
Last Modified: 2012-05-05
I've two columns both columns have this options

Column 1                YES              NO
xxxxxx                chechbox   chechbox

Column 2                YES              NO
yyyyyyyyy          chechbox   chechbox

I want a function an javascript, when I click YES or NO in column 1, automatically check YES or NO (depends what I clicked in column1).

See my screenshot

Thanks
checkbox.gif
0
Comment
Question by:nkoriginal
9 Comments
 
LVL 7

Expert Comment

by:bluV11t
ID: 22822179
Hi!
Could you please post your html so I have somthing to start with?
0
 
LVL 7

Expert Comment

by:aamodt
ID: 22822251
if (document.frmTest.CHKBOX_1.Value == Y)
{
document.frmTest.CHKBOX_2.Checked;
}

dont know exactly.  not so good in javascript but giving you some clue to how to do it.

on the form you will call the function over by onClick(); or something
0
 
LVL 82

Expert Comment

by:hielo
ID: 22822574
try:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
</head>
<body>
<form>
	<table>
		<tr><td><input type="radio" name="col1row1" value="yes" onclick="col2row1[0].checked=true;"/>Yes<input type="radio" name="col1row1" value="no" onclick="col2row1[1].checked=true;"/>No</td><td><input type="radio" name="col2row1" value="yes"/>Yes<input type="radio" name="col2row1" value="no"/>No</td></tr>
		<tr><td><input type="radio" name="col1row2" value="yes" onclick="col2row2[0].checked=true;"/>Yes<input type="radio" name="col1row2" value="no" onclick="col2row2[1].checked=true;"/></td><td><input type="radio" name="col2row2" value="yes"/>Yes<input type="radio" name="col2row2" value="no"/>No</td></tr>
	</table>
</form>
</body>
</html>

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22822610
here's an alternative:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title></title>
<script>
function checkIt(e)
{
 document.getElementById(e).checked=true;
}
</script>
</head>
<body>
<form>
	<table>
		<tr><td><input type="radio" name="col1row1" value="yes" onclick="checkIt('col2row1_yes')"/>Yes<input type="radio" name="col1row1" value="no" onclick="checkIt('col2row1_no')"/>No</td><td><input type="radio" name="col2row1" id="col2row1_yes" value="yes"/>Yes<input type="radio" name="col2row1" id="col2row1_no" value="no"/>No</td></tr>
		<tr><td><input type="radio" name="col1row2" value="yes" onclick="checkIt('col2row2_yes')"/>Yes<input type="radio" name="col1row2" value="no" onclick="checkIt('col2row2_no')"/></td><td><input type="radio" name="col2row2" id="col2row2_yes" value="yes"/>Yes<input type="radio" name="col2row2" id="col2row2_no" value="no"/>No</td></tr>
	</table>
</form>
</body>
</html>

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Author Comment

by:nkoriginal
ID: 22822646
Thanks Guys for your fast help.  (I said checkbox but im using radio, Sorry, but it's doesn't matter, right)

Hielo I tried your way but i dont know what is not working.
Here is my code

(this my radio in the first column)
 
    <td><input type="radio" name="bt1_01_chkctherapy" id="bt1_01_chkctherapy"  value="1" onClick="bt1_01_chkctherapyst.checked=true;"></td>

This is the second column, row 1. I suppose to be checked when I checked 1 column row 1

    <td><div align="center"><input name="bt1_01_chkctherapyst" id="bt1_01_chkctherapyst" type="radio" value="1"></td>

What's wrong??
thanks
0
 
LVL 82

Expert Comment

by:hielo
ID: 22822683
use my second suggestion. Just supply the id of the radio button that you wish to check on the second column.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22822700
assuming your radio buttons on the second column have the same name, this:
<td><input type="radio" name="bt1_01_chkctherapy" id="bt1_01_chkctherapy"  value="1" onClick="bt1_01_chkctherapyst.checked=true;"></td>
 

should have been:

<td><input type="radio" name="bt1_01_chkctherapy" id="bt1_01_chkctherapy"  value="1" onClick="bt1_01_chkctherapyst[0].checked=true;"></td>

Open in new window

0
 
LVL 5

Author Comment

by:nkoriginal
ID: 22822774
Hielo, your second option it worked good.
But I need to change my ID in each input and I can't do that, because I need to keep the same ID for the same input name

Here is the way is worked
    <td><div align="center"><input name="bt1_01_chkctherapyst" id="bt1_01_chkctherapyst1" type="radio" value="1"></td>
    <td><div align="center"><input name="bt1_01_chkctherapyst" id="bt1_01_chkctherapyst2" type="radio" value="0" checked ></td>

but I need to keep in this way

    <td><div align="center"><input name="bt1_01_chkctherapyst" id="bt1_01_chkctherapyst" type="radio" value="1"></td>
    <td><div align="center"><input name="bt1_01_chkctherapyst" id="bt1_01_chkctherapyst" type="radio" value="0" checked ></td>

is any solution to this??
0
 
LVL 82

Expert Comment

by:hielo
ID: 22823621
>>because I need to keep the same ID
You NEED to keep the same NAME, but NOT the ID. As a matter of fact, the ID MUST be unique throughout the whole page.

On another note, in "the rapist" you misspelled "rapist". You used a "y" instead of an "i"
bt1_01_chkctherapyst
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 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

12 Experts available now in Live!

Get 1:1 Help Now