Click here to Skip to main content
15,037,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<html>
<head>
    <meta charset="utf-8">
    <title>C S</title>
	<script type="text/javascript">                   
        
        function vals()      
        {                               
                       
                     if(rbnNumber.value=4)
                     {
                         document.write("You selected Tunisia")                         
                     }
                                              
        }                                      
    </script>
</head>
        <body>  
                <table align = "center">                
                        <tr> 
                        <td>
                        <input type="radio" name="rbnNumber" value="1" />Tasmania<br/>                    
                        </td>                                 
                        </tr>
                        <tr>
                        <td>
                        <input type="radio" name="rbnNumber" value="2" /> Nicaragua<br/>
                        </td>                    
                        </tr>
                        <tr>
                        <td>
                        <input type="radio" name="rbnNumber" value="4" /> Tunisia<br/>
                        </td>                                                    
                        </tr>
                        <tr>
                        <td>
                        <input type="radio" name="rbnNumber" value="5" /> Algeria<br/>
                        </td>                                                    
                        </tr>                                  
                </table>      
            
                <table align = "center">
                       <input type="button" id="vals" Value="Vals" onclick="vals()"/>                                                  
                </table>                          
	</body>
</html>


What I have tried:

document.write does not writes the value of selected radio button
Posted
Updated 3-Jan-18 2:04am
v2

function vals()      
{   
    // use getElementsByName to get all the radio buttons
    var radios = document.getElementsByName("rbnNumber");

    // loop through each one
    for (var i = 0; i < radios.length; i++) {
        var r = radios[i];

        // check the value is 4 and that the radio button is selected
        // note that you use a double equals ("==") to compare
        if (r.value == "4" && r.checked) {
            document.write("You selected Tunisia")
        }
    }
} 
   
You need to use == not =
JavaScript
if(rbnNumber.value=='4')
Also, the value is a string in this context, not an integer, though I dare say JavaScript's implicit conversion can take care of that.
   
v2
the document.write does not prints you selected Tasmania

<html>
    <head>
        <title>Javascript Counts</title>          
        <script type="text/javascript">               
            function vals()      
            {           
            var radios = document.getElementsByName("rbnNumber");   
            for (var i = 0; i < radios.length; i++) {
            var r = radios[i]; 
            if (r.value == "4" && r.checked) {
            document.write("You selected Tasmania")
           }
       </script>
    </head>
    <body>
        <table align = "center">
                        <tr>
                            <td>
                        <a href = "some.html">Previous 
                            </td> 
                            <td>
                        What is the Capital of France  
                            </td> 
                            <td>
                        <a href = "some.html">Next  
                            </td> 
                        </tr>                     
                </table>    
                <table align = "center">                
                        <tr> 
                        <td>
                        <input type="radio" name="rbnNumber" value="4" />Tasmania<br/>                    
                        </td>                                 
                        </tr>
                        <tr>
                        <td>
                        <input type="radio" name="rbnNumber" value="Nicaragua:which is not the correct answer: Try Again" /> Nicaragua<br/>
                        </td>                    
                        </tr>
                        <tr>
                        <td>
                        <input type="radio" name="rbnNumber" value="Tunisia:which is not the correct answer: Try Again" /> Tunisia<br/>
                        </td>                                                    
                        </tr>
                        <tr>
                        <td>
                        <input type="radio" name="rbnNumber" value="Algeria:which is not the correct answer: Try Again" /> Algeria<br/>
                        </td>                                                    
                        </tr>                                  
                </table>      
            
                <table align = "center">
                        <tr>
                        <td align="center">
                       <input type="button" id="btnGetValue"  onclick="vals()" Value="Get Value" />                                                    
                       <p></p>   
                        </td> 
                        </tr>     
                        <tr>                            
                        </tr>
                </table>               
    </body>
</html>
   
Comments
A_Griffin 3-Jan-18 8:42am
   
Just change the value of the Tasmania button from "4" to "Tasmania - correct!" then your function can be:
function vals()
{
var radios = document.getElementsByName("rbnNumber");
for (var i = 0; i < radios.length; i++) {
var r = radios[i];
if (r.checked) {
document.write("You selected " + r.value)
}
}
}
four systems 3-Jan-18 8:47am
   
was js code error works fine now thanks guys
four systems 3-Jan-18 8:57am
   
cool thanks
Please try changing
From
if(rbnNumber.value=4) {
document.write("You selected Tunisia")
}

To
if(rbnNumber.value==4) {
document.write("You selected Tunisia")
}
   
Comments
four systems 5-Jan-18 4:57am
   
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900