PDA

View Full Version : Check a radio button when clicked on input field



PHP_Passion
03-24-2010, 12:48 PM
Hi,

I am new on these forums, I used to use other forums to get some help with some of my stuff but they suddenly disappeared...

So I have the following code:


<TABLE width="100%" class="shared_table">
<thead>
<tr>
<th class="compare"></th>
<th class="compare">Selecteer</th>
</tr>
</thead>
<TR class="odd">
<td><b>lorem ipsum</b> blabla</td>
<td width="15%"><input type="radio" name="ns" value="nee" checked></td>
</TR>
<TR class="even">
<td><b>blabla</b>
<table width="100%">
<tr>
<td width="50%"><span class="cents">bla 1</span>
</td>
<td width="50%"><input name="ns1" type="text" value="">
</td>
</tr>
<tr>
<td width="50%"><span class="cents">bla 2</span>
</td>
<td width="50%"><input name="ns2" type="text" value="">
</td>
</tr>
</table>
</td>
<td width="15%"><input type="radio" name="ns" value="ja"></td>
</TR>
</table>

When one of the input fields is clicked, I want the website to check the last radio button.

So if


<input name="ns1" type="text" value="">

or if


<input name="ns2" type="text" value="">

is clicked i want the website to check


<input type="radio" name="ns" value="ja">

and to uncheck


<input type="radio" name="ns" value="nee" checked>

How do I achieve this?

farridav
03-25-2010, 10:13 AM
Perhaps something like this?, also try and use divs instead of tables :(





<script>
function checkForm(){
var len = document.myForm.ns.length
for (var i = 0; i <len; i++) {if (document.myForm.ns[i].checked){var radio = document.myForm.ns[i].value}}
alert(radio+" is selected")
//any other code here
}
</script>

<TABLE width="100%" class="shared_table">
<thead>
<tr>
<th class="compare"></th>
<th class="compare">Selecteer</th>
</tr>
</thead>
<TR class="odd">
<td><b>lorem ipsum</b> blabla</td>
<form name="myForm">
<td width="15%"><input type="radio" name="ns" value="nee" checked></td>
</TR>
<TR class="even">
<td><b>blabla</b>
<table width="100%">
<tr>
<td width="50%"><span class="cents">bla 1</span></td>
<td width="50%"><input name="ns1" type="text" value="" onfocus="checkForm();" ></td>
</tr>
<tr>
<td width="50%"><span class="cents">bla 2</span></td>
<td width="50%"><input name="ns2" type="text" value="" onfocus="checkForm();" ></td>
</tr>
</table>
</td>
<td width="15%"><input type="radio" name="ns" value="ja" ></td>
</form>
</TR>
</table>