Hi,
I'm self learning javascript - so any pointers are welcomed!!
I have an issue passing a form and array from one function to another.
I tried many variations !
I can't get this to work and I can't get this issue out of my head !!!!
So I'm obviously missing something really simple and can't see it or it can't be done with my limited knowledge.
I know that I can use a cookie to store the array as string - I have this working but I would like not to use cookies if possible.
ok my bare code: - // sets up arrays
-
function generateQuestions(myNumberOfQuestionsAsked)
-
{ // myNumberOfQuestionsAsked is integer value
-
-
var realAnswers =new Array("0010","1010");
-
var answers = new Array();
-
-
for(i=0;i<myNumberOfQuestionsAsked;i++)
-
{ answers[i]= realAnswers[i]; }
-
displayQuestions(answers);
-
}
-
-
-
// creates the form in a table and uses the onclick to pass the form
-
function displayQuestions(answers)
-
{
-
..... within <BODY>
-
myTestWindow.document.write("<TABLE ID='testTable1'>");
-
// create the FORM
-
myTestWindow.document.write("<FORM ID='myForm' NAME='myForm'>");
-
-
// Create the HIDDEN element to hold the array
- myTestWindow.document.write("<INPUT TYPE='hidden' ID='mydata' NAME='mydata' VALUE=''>");
-
myTestWindow.document.write("</FORM>");
-
myTestWindow.document.write("</TABLE>");
-
-
myTestWindow.document.write("<INPUT TYPE='button' value='Mark Your Answers' onClick='testResults(myForm)'>");
-
//the next section is used to refresh the script
-
myTestWindow.document.write("<SCRIPT LANGUAGE='JAVASCRIPT' SRC='quizScript1.js'></SCRIPT>");
-
-
//take the array and put it in the hidden field in a string format
- ansString = unescape(answers.join())
-
myTestWindow.document.myForm.mydata.value=ansString;
-
-
...... closing tags<HERE>
-
}
-
-
// displays the hidden value
-
function testResults(myForm)
-
{
- alert ("You typed: "+myForm.mydata.value);
-
}
Any help would be appreciated!
Thanks!
9 30098
Hi,
I'm self learning javascript - so any pointers are welcomed!!
I have an issue passing a form and array from one function to another.
I tried many variations !
I can't get this to work and I can't get this issue out of my head !!!!
So I'm obviously missing something really simple and can't see it or it can't be done with my limited knowledge.
I know that I can use a cookie to store the array as string - I have this working but I would like not to use cookies if possible.
ok my bare code: - // sets up arrays
-
function generateQuestions(myNumberOfQuestionsAsked)
-
{ // myNumberOfQuestionsAsked is integer value
-
-
var realAnswers =new Array("0010","1010");
-
var answers = new Array();
-
-
for(i=0;i<myNumberOfQuestionsAsked;i++)
-
{ answers[i]= realAnswers[i]; }
-
displayQuestions(answers);
-
}
-
-
-
// creates the form in a table and uses the onclick to pass the form
-
function displayQuestions(answers)
-
{
-
..... within <BODY>
-
myTestWindow.document.write("<TABLE ID='testTable1'>");
-
// create the FORM
-
myTestWindow.document.write("<FORM ID='myForm' NAME='myForm'>");
-
-
// Create the HIDDEN element to hold the array
-
myTestWindow.document.write("<INPUT TYPE='hidden' ID='mydata' NAME='mydata' VALUE=''>");
-
myTestWindow.document.write("</FORM>");
-
myTestWindow.document.write("</TABLE>");
-
-
myTestWindow.document.write("<INPUT TYPE='button' value='Mark Your Answers' onClick='testResults(myForm)'>");
-
//the next section is used to refresh the script
-
myTestWindow.document.write("<SCRIPT LANGUAGE='JAVASCRIPT' SRC='quizScript1.js'></SCRIPT>");
-
-
//take the array and put it in the hidden field in a string format
-
ansString = unescape(answers.join())
-
myTestWindow.document.myForm.mydata.value=ansStrin g;
-
-
...... closing tags<HERE>
-
}
-
-
// displays the hidden value
-
function testResults(myForm)
-
{
-
alert ("You typed: "+myForm.mydata.value);
-
}
Any help would be appreciated!
Thanks!
Sorry! As you can tell I'm new so posted this in the WRONG place!
It has now been moved to the javascript questions area! Although if you know the answer please feel free to comment!
Thanks
What about this piece of code? Did you got any error while trying this code? You can use the hidden variable to set the array values.
Thanks and Regards
Ramanan Kalirajan
No I never got any errors just no data displayed?
I have checked the contents of the array and string as it's passed through the function and it's stored ok. (output in alert's)
It's just when the data is passed in the form I can't see it?
I'm thinking it must be my syntax ? Any suggestions?
Thanks,
Nichola
Dormilich 8,658
Recognized Expert Moderator Expert
do you have a test page available?
Hi,
I got the solution for your question. You will get the array values but not as a array object instead you will get as a String object. The input type hidden variables can hold only string values. When you assign a Array object to the Hidden Variable its implicitly converted to String Value.
This is the following code I tried myself -
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
-
<HTML>
-
<HEAD>
-
<TITLE> New Document </TITLE>
-
<META NAME="Generator" CONTENT="EditPlus">
-
<META NAME="Author" CONTENT="">
-
<META NAME="Keywords" CONTENT="">
-
<META NAME="Description" CONTENT="">
-
<script type="text/javascript">
-
function loadArray(){
-
var realAnswers =new Array("0010","1010");
-
var answers = new Array();
-
for(var i=0;i<realAnswers.length;i++)
-
{
-
answers[i] = realAnswers[i];
-
alert(answers[i]);
-
}
-
document.getElementById('hidArray').value = answers;
-
alert("loaded Successfully "+document.getElementById('hidArray').value);
-
}
-
function displayArray()
-
{
-
var myhidObj = new Array (document.getElementById('hidArray').value);
-
alert("myhidObj = "+myhidObj);
-
alert("length = "+myhidObj.length);
-
for(var i=0;i<myhidObj.length;i++)
-
alert(myhidObj[i]);
-
}
-
</script>
-
</HEAD>
-
<BODY>
-
<form id="myForm" name="myForm">
-
<input type="hidden" name="hidArray" id="hidArray" value=""/>
-
</form>
-
<input type="button" value="loadArray" onclick="loadArray();" />
-
<input type="button" value="displayArray" onclick="displayArray();" />
-
</BODY>
-
</HTML>
Thanks and Regards
Ramanan Kalirajan
Oh that works a treat! Magic!!
However I want to run the javascript in a .js file. When I do I'm getting issues with the document values. When in displayQuestion s(), I create my form and new window. I can access all elements, however when I'm trying to access document.getEle mentById('hidAr ray').value from another function (displayArray() I'm getting no value?
I think the document is local to that function? How would I pass the document or would I be better passing the form that way I can access other elements too?
I have tried passing the form and accessing the element by form.elements['hidArray'] and again I see no output?
Must be my syntax - I'm sure you can pass the form this way!
My code is below: - function displayQuestions(selectedQuestions)
-
{
-
//close the questions window
-
alert("The Questions will open in a new Window - Click OK to proceed ");
-
var length = selectedQuestions.length;
-
myTestWindow = window.open('','myTestWindow','scrollbars=yes,width=800,height=650,left=20,top=10');
-
myTestWindow.focus();
-
//create the page dynamically
-
myTestWindow.document.write("<HTML><HEAD><TITLE>The Test Page</TITLE></HEAD>");
-
myTestWindow.document.write("<BODY bgcolor='#003366' text='#FFFFFF' >");
-
-
myTestWindow.document.write("<TABLE ID='testTable1' border=1 bordercolor='white' CELLPADING='' CELLSPACING='0' background= '' width='90%' height='90%'ALIGN='CENTER' bgcolor='#003366'>");
-
myTestWindow.document.write("<TR width='100%' height='20%'>");
-
myTestWindow.document.write("<TD colspan=10 align=center><font color='#FFFFFF' face='Verdana, Arial, Helvetica, sans-serif'");
-
-
myTestWindow.document.write("</TD>");
-
myTestWindow.document.write("</TR>");
-
myTestWindow.document.write("<form id='myForm' name='myForm'>");
-
myTestWindow.document.write("<input type='hidden' name='hidArray' id='hidArray' value=''/> ");
-
/*using a for loop to dynamically generate rows and columns equal to the number of questions */
-
for (i=0;i<length;i++)
-
{
-
myTestWindow.document.write("<TR>");
-
myTestWindow.document.write("<TD valign=top id ='questionsColumn"+i+"' name= 'questionsColumn"+i+"' colspan=4>");
-
myTestWindow.document.write("<P><font color='#FFFFFF' face='Verdana, Arial, Helvetica, sans-serif'> Convert this to Binary</TD>");
-
-
myTestWindow.document.write("<TD valign=top align = center colspan=2>");
-
myTestWindow.document.write("<INPUT TYPE='text' ID='text"+i+"' NAME='text"+i+"'>");
-
myTestWindow.document.write("</TD>");
-
myTestWindow.document.write("</TR>");
-
}//for
-
-
myTestWindow.document.write("</form>");
-
myTestWindow.document.write("</TABLE>");
-
-
ans= getAnswers(length);
-
myTestWindow.document.getElementById('hidArray').value = ans;
-
-
alert("loaded Successfully "+myTestWindow.document.getElementById('hidArray').value);
-
myTestWindow.document.write("<input type='button' value='displayArray' onclick='displayArray();' />");
-
myTestWindow.document.write("<SCRIPT LANGUAGE='JAVASCRIPT' SRC='quizScript1.js'></SCRIPT>");
-
}
-
-
-
-
function displayArray()
-
{
-
alert("HERE");
-
var myhidObj = new Array (myTestWindow.document.getElementById('hidArray').value);
-
alert("myhidObj = "+myhidObj);
-
alert("length = "+myhidObj.length);
-
for(var i=0;i<myhidObj.length;i++)
-
alert("FINISHED"+myhidObj[i]);
-
}
-
-
function getAnswers(length)
-
{
-
alert("getAns1 the length is"+length);
-
var realAnswers =new Array("0010","1010","0111","0011","0001","0100","0101");
-
var answers = new Array();
-
-
for(i=0;i<length;i++)
-
{
-
answers[i]= realAnswers[i];
-
}
-
return answers;
-
}
acoder 16,027
Recognized Expert Moderator MVP
displayArray() from the popup window expects that it is defined within the window. Since it's actually in the parent window, change it to window.opener.d isplayArray().
PS. please use code tags when posting code.
Whoo hoo!!
Fantastic!
There was no way I was even thinking like that!
Thank you very much!
I just modified
document.getEle mentById('hidAr ray').value
to
window.opener.m yTestWindow.doc ument.getElemen tById('hidArray ').value
and it works perfect!
Much appreciated!
I will add code tags for any other posts - thanks for the pointer.
N
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Bharat Suneja |
last post by:
Hi All,
I'm running into issues with passing the distinguishedName of a user as a
hidden form field in a form from one page to the result page.
Page 1: User enters user name in a form field
Page 2: Queries AD, gets distinguishedName, binds to the user.
Need to add more info to the user if attributes not present.
Relevant portion of FORM:
|
by: Jaygo |
last post by:
I want to get the values from various page links to
a hidden field in a single online form. So if visitor A links
to the form from apples.htm and visitor B links to the
form from oranges.htm the value in the db table field would be
Apples or Oranges. Any pointers appreciated.
TIA John
|
by: GavMc |
last post by:
Hello I am new to internet programming and wonder if anyone can help me with
this....
I am trying to pass a hidden field value on a form into another field on the
form so that it can then be inserted in the database, to enable me then to
reference that number at a later date.
(The hidden value (1 for example) would then automatically get passed to the
other input field.)
|
by: Paul |
last post by:
I want to set the page title and/or a form hidden field programatically
through ASP.Net.
I do not want to use something like...
<% sTitle ="My Title" %>
<html><title><%=sTitle%></title>.....
I want to completely seperate the code from presentation.
I would like to do the same thing for a value of a hidden form field but
|
by: Alan Silver |
last post by:
Hello,
I have a page that gets passed an ID in the query string. It then uses
this ID to pull info out of a database and populate controls on the
page. When the page is posted back, the query string is not going to be
there any more, so I need some way of storing the ID.
What's the best way of doing this? The obvious thought is a hidden form
field, but there doesn't seem to be a web control for this. Do I just
use an ordinary HTML...
| |
by: xenophon |
last post by:
I add a Hidden form field to my asp.net page in the codebehind, the
Value peoperty is set to 0 and EnableViewState is set to true. Then I
add a LiteralControl with JavaScript that says to set the value to 1.
If I set view the value of th ehidden form field in the debugger
during the course of a PostBack, the value is still set to 0.
Are there any samples of setting a vlaue with JavaScript and reading
the new value in ASP.NET?
Thanks.
|
by: xenophon |
last post by:
I added a Hidden Form Field to a form in the code behind.
The value is being set in JavaScript client-side, but it is not
persisting to the server in the PostBack.
I know the value is being set properly because it displays in the
document.write method.
Create a simple page and paste the below in the code-behind (ASP.NET
1.1-SP1)
using System;
|
by: windsorben |
last post by:
How would I pass the student's final score to a hidden form field?
<script language="javascript" type="text/javascript">
<!--
var score = 0;
|
by: Noah |
last post by:
Hello.
I have a form like so:
<FORM NAME="form1" METHOD = "post">
<SELECT NAME="select">
<OPTION VALUE = "1">Option 1</OPTION>
<OPTION VALUE = "2">Option 2</OPTION>
|
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...
|
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...
| |
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,...
|
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...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |