473,657 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using javascript to access array of textfields

25 New Member
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 2474
iam_clint
1,208 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Moderator MVP
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
25205
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 *** Don't just participate in USENET...get rewarded for it!
6
23743
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 in advance. Sergio Otoya.
2
17633
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 all the files in the directory and select the one with the highest week number then display in the browser window. A brief search hasn't revealed any code to do this so I just want to know if it is possible and what the function names are that...
1
1480
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 using a decrementing array to locate the foldername within the URL. is there a better way in javascript? does anyone know where i can find a currently working script i might look at?
4
44063
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 window is to redirect user to the one of available servers. so i need to check which server is available. i have decided to download some 1pixel image from every server and if it was downloaded
3
45960
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 to find a reference to request or response objects in JavaScript either in Google Groups or O'Reilly's "JavaScript the Definitive Guide" (surely it should be in the latter, huh?) I know I could use a hidden form element but the data would be a...
1
3118
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 elements and would like to seek a solution for this. I had looked through several articles for accessing programatically-created dynamic elements such as: 1)
5
1406
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? Thanks in advance for your help! Steve
1
1752
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 declared this to an array like <input type="checkbox" name="C3" value="<?php echo $arr;?>"and i want to get these checked values using javascript. but when I am doing document.formname.C3.checked , it gives me javascript error.
2
1395
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How can I prevent access to a web page by using javascript? ----------------------------------------------------------------------- In practice you can't. While you could create a suitable encryption system with a password in the page, the level of support you need to do this means it's always simpler to do it server-side. Anything that "protects" a page...
0
8385
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8723
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8502
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7316
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.