Solved

please see what is wrong with my JS code

Posted on 2012-03-20
3
173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

733 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