?
Solved

please see what is wrong with my JS code

Posted on 2012-03-20
3
Medium Priority
?
176 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 1000 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 1000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

771 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