SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    radio button checked for voting

    ........
    <tr>
    <td><li><label for="reception" id="recep"> Efficiency of Reception </label></li></td>
    <td align="center"><input type="radio" value="1" name="myradiobutton1" id="myradiobutton1" ></td>
    <td align="center"><input type="radio" value="2" name="myradiobutton1" id="myradiobutton1"></td>
    <td align="center"><input type="radio" value="3" name="myradiobutton1" id="myradiobutton1"></td>
    <td align="center"><input type="radio" value="4" name="myradiobutton1" id="myradiobutton1"></td>
    <td width="119" align="right"><img src="images/vot_diag.jpg" width="119" height="16" /></td>
    <td align="right">8.2</td>
    </tr>.........


    Now i have many radio buttons and i want to make a voting system for multiple questions..

    this is a row of my radios ...

    i Want to know which radio button is checked.. so if check radio button1 on value =1 i want to know that the value checked is equal to 1

    if the user press on radiobutton1 value=2 i want to return the value2


    Where is the error in my code now??

    function fd1()
    {
    do {

    var p
    p=document.getElementById('myradiobutton1').value;

    document.write(p);



    }while(0)
    }

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    There are a number of problems here.

    You can't work with radio buttons until after the page has finished loading.
    document.write will clear the screen if its used after the page has finished loading.

    The id attribute is to provide a unique identifier for a certain element on the page. This means that they must be unique. Duplicates are not allowed. In this case the id attributes aren't required at all, so they can be removed.

    Here is how you can get the currently selected radio button.
    You get the form, get the radio elements, the loop through each one looking the one that's currently checked.

    Code html4strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>Test</title>
    <style type="text/css">
    </style>
    </head>
    <body>
    <form id="myForm">
    <ul>
    <table>
    	<tr>
    		<td><li><label for="reception" id="recep"> Efficiency of Reception </label></li></td>
    		<td align="center"><input type="radio" value="1" name="myradiobutton1"></td>
    		<td align="center"><input type="radio" value="2" name="myradiobutton1"></td>
    		<td align="center"><input type="radio" value="3" name="myradiobutton1"></td>
    		<td align="center"><input type="radio" value="4" name="myradiobutton1"></td>
    		<td width="119" align="right"><img src="images/vot_diag.jpg" width="119" height="16" /></td>
    		<td align="right">8.2</td>
    	</tr>
    </table>
    </ul>
    </form>
    <button onclick="fd1()">Show Radio Value</button>
    <script>
    function fd1() {
    	var form = document.getElementById('myForm');
    	var radios = form.elements.myradiobutton1;
    	var radioValue;
    	var i;
    	for (var i = 0; i < radios.length; i += 1) {
    	    if (radios[i].checked) {
    			radioValue = radios[i].value;
    		}
    	}
    	var text = document.createTextNode(radioValue);
    	document.body.appendChild(text);
    }
    </script>
    </body>
    </html>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •