Results 1 to 2 of 2

Thread: Check a radio button when clicked on input field

  1. #1
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Check a radio button when clicked on input field

    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:

    HTML 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

    HTML Code:
    <input name="ns1" type="text" value="">
    or if

    HTML Code:
    <input name="ns2" type="text" value="">
    is clicked i want the website to check

    HTML Code:
    <input type="radio" name="ns" value="ja">
    and to uncheck

    HTML Code:
    <input type="radio" name="ns" value="nee" checked>
    How do I achieve this?

  2. #2
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default this works

    Perhaps something like this?, also try and use divs instead of tables


    Code:
    <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>
    Last edited by jscheuer1; 03-26-2010 at 04:47 AM. Reason: format code

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •