473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

using javascript to access array of textfields

25
Hi there, newish to javascript so grateful for any help.

I am creating a test generator and have an array of textfields ("answer[]") created by a simple piece of PHP code. As you can see, I have radio buttons corresponding to each text field, which is a potential answer to a multi choice question. I already have working code which ensures that an answer is selected and not the default option. However, I also want to validate that when an answer is selected, the corresponding text field is not blank. I am having problems accessing the values of each text field in the array.

Here's the php/html code....

Expand|Select|Wrap|Line Numbers
  1. <p>
  2. Enter the answer options and ensure the radio button selected is the correct answer. <br/><br/>
  3. <?php
  4. //Generate form
  5. $count=5;
  6. $name=array("A","B","C","D","E");
  7. for ($i=0;$i<$count;$i++) {
  8. echo "<input type='radio' name='option' value='$i'>$name[$i] <input type='text' name='answer[$i]' size='80'><br/>";
  9. }
  10. ?>
  11. <input type="radio" name="option" value="No Answer" checked="checked" />No Answer selected
  12.  
and here's the JS function to check it.....

Expand|Select|Wrap|Line Numbers
  1. function validateCorrectAnswer(form){
  2. var error="";
  3. var none="No Answer";
  4. var ansText ="";
  5. var selected;
  6.  
  7. for (var i=0; i<form.option.length; i++) {
  8. if (form.option[i].checked)
  9. {
  10. selected = form.option[i].value ;
  11. if (selected==none)
  12. error = "You didn't select a correct answer.\n";
  13. else if(form.elements['answer[i]'].value=="")
  14. error = "You didn't enter any text for the correct
  15. answer.\n";
  16. }
  17. }
  18. return error;
  19. }
Aug 1 '07 #1
3 2452
iam_clint
1,208 Expert 1GB
Heres a start for ya
Expand|Select|Wrap|Line Numbers
  1. function validateCorrectAnswer(form){
  2. var error="";
  3. var none="No Answer";
  4. var ansText ="";
  5. var selected;
  6. var selOptions = document.getElementsByTagName("input");
  7. for (var i=0; i<selOptions; i++) {
  8. if (selOptions[i].getAttribute("type")=="radio" && selOptions[i].checked) {
  9. selected = selOptions[i].value ;
  10. if (selected==none) {
  11. error = "You didn't select a correct answer.\n";
  12. else if(form.elements['answer[i]'].value=="")
  13. error = "You didn't enter any text for the correct
  14. answer.\n";
  15. }
  16. }
  17. return error;
  18. }
  19.  
Aug 1 '07 #2
nicky77
25
Thanks for the reply iam_clint. Still having a problem, however. When the function is called it never satisfies the first If statement, for some reason. Can't see what the problem is and i'm getting no error messages (there was a stray bracket in the original code but that's not the problem). Any further help would be appreciated. Cheers
Aug 1 '07 #3
acoder
16,027 Expert Mod 8TB
In your original code, you could change line 13 to:
Expand|Select|Wrap|Line Numbers
  1. else if(form.elements['answer['+i+']'].value=="")
Aug 2 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: sriram pasham | last post by:
Hi Could any one tell me how to access(read) a registry key using javascript from a webpage(ASP page). Thanks in advance. Sriram *** Sent via Developersdex http://www.developersdex.com ***...
6
by: Sergio Otoya | last post by:
Hi all, Is there any way of copying a file using javascript, not using the Filesystemobject (ActiveX). I need this to run in Windows and MACS. Any help would be greatly appreciated. Thanks...
2
by: Dean | last post by:
Hi I've got a question relating to using Javascript on an Intranet. I have a directory with a list of files in the format week36.xls, week37.xls and I want to write a script that will scan...
1
by: ThomAla | last post by:
i'd like to find a javascript that can specify the foldername a page finds itself within. i'm not a Javascript expert, but i know this can be done by saving the full URL into a string, then...
4
by: zborisau | last post by:
Hey good people, I've been given a problem to solve recently - and stuck with the solution for a good 4 days already. i have a link which leads to popup window. the purpose of that popup...
3
by: news.rcn.com | last post by:
How can I access the request and response object for a page using javascript. I want to stick some data on with something like request.setAttribute( "User's choice for later use" ). I can't seem...
1
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
5
by: Steve Kershaw | last post by:
I have a problem in that I need to access the value (text) in an asp:textbox using javascript. Is there any way to do this or am I all wet? Do I have to use VBScript to access asp: objects? ...
1
by: Amar | last post by:
Hi All, In my php application , I am using an sql query and this query is returning me a now of rows and I am storing these values in check box. Here I am using a single check box and...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How can I prevent access to a web page by using javascript?...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.