Solved

please see what is wrong with my JS code

Posted on 2012-03-20
3
170 Views
Last Modified: 2012-03-21
Hi,

The following code is giving me errors.
Can someone please advise what I am doing wrong?

<html>
<head><title>Id and Name Attributes</title>

<script type="text/javascript">

function validateAnswer() {

      if (document.form[0].["group1"].value="Philip")
                  {
            alert("Correct!");
            document.getElementById("Philip").checked;
            document.getElementById("text1").value="Philip";
                  }
      else
                  {
            alert("False!");
            document.getElementById("Philip").checked;
            document.getElementById("text1").value="Philip";
                  }
}
            


</script>


</head>


<body>

<form id="form1">

<input type="radio" name="group1" id="Philip" value="Philip" />Philip<br />
<input type="radio" name="group1" id="Joe" value="Joe" checked />Joe <br />
<input type="radio" name="group1" id="Alex" value="Alex" />Alex <br />
<input type="text" name="text1" id="text1" value="text to go here" />
<input type="button" name="button1" id="button1" value="submit" onClick="validateAnswer(this)" />



</form>

</body>
</html>

thanks
0
Comment
Question by:jecommera
3 Comments
 
LVL 7

Accepted Solution

by:
Lalit Chandra earned 250 total points
ID: 37745254
Unfortunately, Extracting the values of the Radio button is not so much straightforward.
See the url to more and precise help
http://www.webdevelopersnotes.com/tips/html/finding_the_value_of_a_radio_button.php3
0
 
LVL 31

Assisted Solution

by:GwynforWeb
GwynforWeb earned 250 total points
ID: 37745816
Firstly its is

document.forms[0]  not document.form[0]

Secondly are trying to address a radio group as if it was a Select Box.

This might do the job for you though:-

<script type="text/javascript">

function validateAnswer() {
 if (document.getElementById("Philip").checked) alert('correct');
 else
 {
   alert("false")
   document.getElementById("Philip").checked=true
  }
}
 
</script>

<form id="form1">
  <p><input type="radio" name="group1" id="Philip" value="Philip" />Philip<br />
  <input type="radio" name="group1" id="Joe" value="Joe" checked />Joe <br />
  <input type="radio" name="group1" id="Alex" value="Alex" />Alex <br />
  <input type="text" name="text1" id="text1" value="text to go here" />  
  <input type="button"
  name="button1" id="button1" value="submit" onClick="validateAnswer(this)" /> </p>
</form>
0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 37745910
<!--

jQuery is your friend with a one-liner:

$("input[name=group1]:checked").val()

-->
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>
 
  <script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
 
<script type='text/javascript'>//<![CDATA[
$(function(){
$("#button1").click(validateAnswer);

function validateAnswer() {
    var answer = "Philip";
    $("#text1").val(answer)
    var message = "False!";
    if ($("input[name=group1]:checked").val() == answer) message = "Correct!";

    alert(message);
}
});//]]>  

</script>

</head>
<body>
  <form id="form1">
<input type="radio" name="group1" id="Philip" value="Philip" />Philip<br />
<input type="radio" name="group1" id="Joe" value="Joe" checked />Joe <br />
<input type="radio" name="group1" id="Alex" value="Alex" />Alex <br />
<input type="text" name="text1" id="text1" value="text to go here" />
<input type="button" name="button1" id="button1" value="submit" onClick="validateAnswer(this)" />
</form>
 
</body>
</html>
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

777 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