/    Sign up×
Community /Pin to ProfileBookmark

getObj with "myForm.quest1"

I am trying to get this to work. I am trying to dynamically create a test from an array of questions. I am using a external script, because otherwise, I would dynamically have to recreate all of the script, because the first page that loads is destroyed by the dynamically created 2nd page. Also, the problem im running into is getting the getObj to work from a specific form. I want it to be in Javascript/HTML. Here are the 2 files involved:

Please see the errors in comments:

[code=php]
function checkTest(myForm)
{
for(x=0; x<questions; x++)
{
//theSearch=”quest” + x
//alert(“quest1”);
//alert(myForm.quest1.value);
//alert(getObj(“myForm.quest1”));
//alert(getObj(“myForm.quest1”).value);
theQuest=getObj(myForm.quest + x);
alert(theQuest.value); //error here?????????
if(theQuest.value == “”) //want to use getObj if possible
{
alert(“Fill all”);
}
}
}

function getObj(name)
{
//alert(name);
if (document.getElementById)
{
this.obj = document.getElementById(name);
this.style = document.getElementById(name).style; //should be good, but i get error here…………….
}
else if (document.all)
{
this.obj = document.all[name];
this.style = document.all[name].style;
}
else if (document.layers)
{
this.obj = document.layers[name];
this.style = document.layers[name];
}
}
[/code]

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@TageMay 29.2004 — Are you trying to achieve something like this?[code=php]<html>
<head>
<title></title>
<script type="text/javascript">
<!--
function checkTest(myForm){
for(x=0; x<myForm.elements.length; x++){
if(myForm[x].value==""){
alert("Fill all");}}}
//-->
</script>
</head>
<body>
<form name="theForm">
<input type="text" value="hello" name="quest0">
<input type="text" value="goodbye" name="quest1">
<input type="button" value="click" onClick="checkTest(this.form)">
</form>
</body>
</html>[/code]
I don't know the compatibilities of this script I just wrote, but I couldn't quite make out your script there. Sorry.

Tage

Edit: Or something like this...[code=php]<html>
<head>
<title></title>
<script type="text/javascript">
<!--
var questions=2;
function checkTest(myForm){
for(x=0; x<questions; x++){
if(eval("myForm.quest"+x+".value")==""){
alert("Fill all");}}}
//-->
</script>
</head>
<body>
<form name="theForm">
<input type="text" value="hello" name="quest0">
<input type="text" value="goodbye" name="quest1">
<input type="button" value="click" onClick="checkTest(this.form)">
</form>
</body>
</html>[/code]
Also, I would like to say (and I mean no disrespect) that your post was quite uninformative and it was missing the var "questions" and what event handler handled the function checkTest(). I had to guess on what I thought you wanted. So it would help you and us a lot more if you could be more specific about your script, problem, and what you are trying to achieve. There might be a simpler way.

EDIT AGAIN: or maybe this[code=php]<html>
<head>
<title></title>
<script type="text/javascript">
<!--
var questions=2;
function checkTest(myForm){
for(x=0; x<questions; x++){
if(myForm.elements["quest"+x].value==""){
alert("Fill all");}}}
//-->
</script>
</head>
<body>
<form name="theForm">
<input type="text" value="hello" name="quest0">
<input type="text" value="goodbye" name="quest1">
<input type="button" value="click" onClick="checkTest(this.form)">
</form>
</body>
</html>[/code]

Tage
Copy linkTweet thisAlerts:
@rex64authorJun 01.2004 — Sorry I didnt give you enough info. I am trying to dynamically create a test from an array of questions. I am using a external script, because otherwise, I would dynamically have to recreate all of the script, because the first page that loads is destroyed by the dynamically created 2nd page. Also, the problem im running into is getting the getObj to work from a specific form. I want it to be in Javascript/HTML. Here are the 2 files involved:
[code=php]
<html>
<head>

<style>
.question{
color:black;
font:bold;
}
</style>

<title>StudyX.com</title>

<script>
questions=4;
questionArray = [
['What is your name:','Jeff', 'text'],
['What gender:','Male', 'text'],
['What do you ride:','I have a car', 'text'],
['What is favorite color:','black', 'text'],
];

function generateCode()
{
scr="script";
//alert(scr);
//alert("<" + scr);
//alert("<" & scr & " src='studyx.js'></" & scr & ">");
document.write("<" + scr + " src='studyx.js'></" + scr + ">");
document.write("<form name='myForm'>");
for(x=0; x<questions; x++)
{
if(questionArray[x][2]=="text")
{
document.write("<style>.question{ color:black;font:bold;}</style>");
document.write("<div class='question'>");
document.write(questionArray[x][0]);
document.write("</div>");
document.write("<input type='text' name='quest");
document.write(x);
document.write("' value='x'><br><br>");
}
}
document.write("</form>");
document.write("<input type='submit' name='submit' onclick='checkTest(myForm)'>");
}

</script>

</head>

<body onload="generateCode()">
This is a test.

<form>
<div class="question">What is your name:</div>
<input type="text" name="firstname"><br><br>

<div class="question">What gender:</div>
<input type="radio" name="sex" value="male"> Male<br>
<input type="radio" name="sex" value="female"> Female<br>

<br><br>

<div class="question">What do you ride:</div>
<input type="checkbox" name="bike">I have a bike<br>
<input type="checkbox" name="car">I have a car<br>


<input type="submit" value="Submit" onclick="checkTest()">


</form>

<body>

</html>
[/code]


[code=php]
questions=4;

function checkTest(myForm)
{
for(x=0; x<questions; x++)
{
//theSearch="quest" + x
//alert("quest1");
//alert(myForm.quest1.value);
//alert(getObj("myForm.quest1"));
//alert(getObj("myForm.quest1").value);
theQuest=getObj("quest" + x, myForm);
//alert(theQuest);
alert(theQuest.value); //error here?
if(theQuest.value == "") //want to use getObj if possible
{
alert("Fill all");
}
}
}

function getObjFromForm(name, form)
{
//alert(name);
if (document.getElementById)
{
this.obj=form.elements[name]
//this.obj = document.getElementById(name);
this.style = document.getElementById(name).style; //should be good, but i get error here
}
else if (document.all)
{
this.obj = document.all[name];
this.style = document.all[name].style;
}
else if (document.layers)
{
this.obj = document.layers[name];
this.style = document.layers[name];
}
}

[/code]
Copy linkTweet thisAlerts:
@rex64authorJun 01.2004 — any ideas how to make this work? Especially, I would like browser compatibility if possible. Thanks.
Copy linkTweet thisAlerts:
@rex64authorJun 02.2004 — See above problem.
Copy linkTweet thisAlerts:
@TageJun 02.2004 — You can't call the document.write() function after a page loads or else it will overwrite the whole page. That's part of why you're getting an error I think. My brain is exhausted at the moment; so I really can't make out your code at the moment. I'll try later today if someone else hasn't gotten to it.

Tage
Copy linkTweet thisAlerts:
@rex64authorJun 02.2004 — I know you can not call it afterwards, that is why I re-write the page, and also use an external script to resolve problems that arise from the page being erased and re-written.
Copy linkTweet thisAlerts:
@TageJun 02.2004 — Sorry, my mind was fatigued, honest! Lol. Try returning the objects in the getObj() function. Also, your function name does not match the function name that is being called. If you're trying for compatibility, I would suggest using comment tags (<!-- and --> you comment the --> in the script tag though, so it's //-->) in style sheets and script tags. Also, declaring their type. CSS would be text/css and javascript would be text/javascript. Also, you may want to declare a [url=http://www.w3.org/QA/Tips/Doctype]doctype[/url] and what [url=http://www.w3.org/TR/REC-html40/charset.html]charset[/url] to decode your page in. I don't see why you made "script" into a var? There really is no reason to do that that I can see. Also, you didn't put <html><body> and </body></html> in the document.write() area. It might cause rendering problems somewhere, who knows.[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.question{
color:black;
font:bold;}
-->
</style>
<title>StudyX.com</title>
<script type="text/javascript">
<!--
questions=4;
questionArray=[
["What is your name:","Jeff", "text"],
["What gender:","Male", "text"],
["What do you ride:","I have a car", "text"],
["What is favorite color:","black", "text"]];

function generateCode(){
document.write("<html><body>");
document.write("<script type='text/javascript' src='studyx.js'></script>");
document.write("<form name='myForm'>");
for(x=0; x<questions; x++){
if(questionArray[x][2]=="text"){
document.write("<style type='text/css'><!--n.question{ color:black;font:bold;}n--></style>");
document.write("<div class='question'>");
document.write(questionArray[x][0]);
document.write("</div>");
document.write("<input type='text' name='quest");
document.write(x);
document.write("' value='x'><br><br>");}}
document.write("</form>");
document.write("<input type='submit' name='submit' onclick='checkTest(myForm)'></body></html>");}
//-->
</script>
</head>
<body onload="generateCode()">
This is a test.
<form>
<div class="question">What is your name:</div>
<input type="text" name="firstname"><br><br>
<div class="question">What gender:</div>
<input type="radio" name="sex" value="male"> Male<br>
<input type="radio" name="sex" value="female"> Female<br>
<br><br>
<div class="question">What do you ride:</div>
<input type="checkbox" name="bike">I have a bike<br>
<input type="checkbox" name="car">I have a car<br>
<input type="submit" value="Submit" onclick="checkTest()">
</form>
</body>
</html>[/code]
studyx.js:[code=php]questions=4;

function checkTest(myForm){

for(x=0; x<questions; x++){
theQuest=getObj("quest"+x);
alert(theQuest.value);
if(theQuest.value==""){
alert("Fill all");}}}

function getObj(name){
if(document.getElementById){
return document.getElementById(name);}
else if(document.all){
return document.all[name];}
else if (document.layers){
return document.layers[name];}}[/code]
Tage
Copy linkTweet thisAlerts:
@rex64authorJun 03.2004 — When I was using script IE was thinkging I was opening or closing another script and getting confused.
×

Success!

Help @rex64 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...