Solved

please see what is wrong with my JS code

Posted on 2012-03-20
3
168 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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 …
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now