Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
| Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 16th, 2007, 06:35 AM
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default problems with JavaScript Code


i am having problems making this work...
 please help..


 
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>



<script language="JavaScript"><!--
function VerifyData() {
    for (var i=0;i<2;i++) {
        if (document.testa[i].checked)
            return true;
    }
    alert('You must chose a template for section testa');
    return false;

    for (var i=0;i<2;i++) {
        if (document.testb[i].checked)
            return true;
    }
    alert('You must chose a template for section testb');
    return false;
}


//-->




</script>




</head>
<body>
 
<form name="Template" method="post" action="next.asp"  onSubmit="return VerifyData();">


         a<input type="radio" name="testa" id="testa" value="1" /> 


         b<input type="radio" name="testa" id="testa" value="3" /> 

<br />

            c<input type="radio" name="testb" id="testb" value="1" /> 


         d<input type="radio" name="testb" id="testb" value="3" /> 
<br />

        <input type="submit" value="go" />

         </form>

</body>
</html>
  #2 (permalink)  
Old February 16th, 2007, 07:00 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

There are two obvious mistakes. Firstly you can't access radio groups by index. You need to use getElementsByName('testa') to get a collection of the relevant group and then loop through this collection as you have done:
Code:
var colGroup = getElementsByName('testa');
for (var i = 0; i < colGroup.length; i++)
{
  if (colGroup.checked) return true;
}
The second error is that your function will always return after testing the first group. You need to change it so that it returns false if it fails but if it succeeds then does the second check against the other group.

--

Joe (Microsoft MVP - XML)
  #3 (permalink)  
Old February 16th, 2007, 07:20 AM
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I have tried to use your code to get it to work..

but now i get the error message whether one radio is selected or not..

Code:
<script language="JavaScript"><!--
function VerifyData() {




var colGroup = document.getElementsByName('testa');
for (var i = 0; i < colGroup.length; i++) {
  if (colGroup.checked) 
  return true;
  }
  alert('You must chose a template for section 1');
    return false;
}


//-->




</script>
  #4 (permalink)  
Old February 16th, 2007, 07:26 AM
Authorized User
 
Join Date: Oct 2006
Location: , , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok this works for one..


<script language="JavaScript"><!--
function VerifyData() {




var colGroup = document.getElementsByName('testa');
for (var i = 0; i < colGroup.length; i++) {
  if (colGroup[i].checked)
  return true;
  }
  alert('You must chose a template for section 1');
    return false;
}


//-->




</script>

so how then would i make it work for 2..

when i cant do this..

<script language="JavaScript"><!--
function VerifyData() {




var colGroup = document.getElementsByName('testa');
for (var i = 0; i < colGroup.length; i++) {
  if (colGroup[i].checked)
  return true;
  }
  alert('You must chose a template for section 1');
    return false;


    var colGroup2 = document.getElementsByName('testb');
for (var a = 0; a < colGroup2.length; a++) {
  if (colGroup2[a].checked)
  return true;
  }
  alert('You must chose a template for section 2');
    return false;


}


//-->




</script>

  #5 (permalink)  
Old February 16th, 2007, 08:57 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Sorry missed the [i] bit.

--

Joe (Microsoft MVP - XML)
  #6 (permalink)  
Old March 6th, 2007, 04:44 PM
Registered User
 
Join Date: Jul 2005
Location: New York, NY, USA.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Why did you type this?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

What is it for?


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems parsing XML Feed in Javascript kwilliams Classic ASP Professional 0 December 18th, 2007 06:33 PM
Problems parsing XML Feed in Javascript kwilliams Javascript 1 November 7th, 2007 05:59 PM
Problems passing xsl parameters from javascript mxxz XSLT 4 November 3rd, 2006 11:21 AM
prevent javascript injection attacks and problems crmpicco Javascript 1 April 20th, 2006 11:05 AM
Javascript problems LIONEL33 JSP Basics 1 March 6th, 2005 09:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.