Solved

please see what is wrong with my JS code

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

790 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