Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems with input form !

Status
Not open for further replies.

aladdin

Instructor
Aug 7, 2002
9
GB
Hello,

I have same basic (beginner's) problem with JS.

sample from the JS ....

------------------------------------------------------------
function recalc(form)
{

for ... {
var quantity;

XXX: quantity=eval("form.quantity"+i+".value");
parent.izd.kol = quantity;

.... end for }

}

--------------
htmout = '<div align=&quot;center&quot;><input type=&quot;text&quot; name=&quot;quantity' + i +
'&quot; size=&quot;3&quot; maxlength=&quot;3&quot; value=&quot;' + parent.izd.kol +
'&quot;>  <input type=&quot;button&quot; value=&quot;Recalc&quot; onClick=&quot;recalc(this.form)&quot;>'


.
.
.
.

for i .... {

document.write(htmout);

end for ... }

--------------> This portion of JS works fine, I got all the right values displayed. All the variables are assigned at the beginning (array) !

but I have a problem with recalc(): how can I access the value of 'quantity'+i when is changed.
recalc() always ends up with an error: object required, error in line XXX.
Can I access and assign a value of (input type=text....) with onChange event to 'parent.izd.kol' ???
Both parts of JS resides in the same HTML file.

To shorten: I have a problem accessing (and assigning) values to/from input form (type=text,name=smthng+variable) if I want to use a loop and variables to have different set of names for input form !

I know that I'm probably doing something wrong but I'm all out of clues !!


If you're willing to help and need more info, just let me know !

Thanks for help and suggestions !
 
could you please repost only the code, but UNcheck the &quot;process tgml&quot; box ?
anyway i guess the error comes from the names of your text boxes
try to loop over the form and alert form.elements[index].name and check if they are &quot;quantity1&quot; .... sounds like you'll get &quot;quantityi&quot; - but still, as your code is not diplayed properly, it would be better to repost it
 
aladdin,

Use the following:

quantity=eval(&quot;form.quantity&quot; + i).value


Mise Le Meas,

Mighty :)
 
1. HTML file

function izdelek(ime,kup,kol)
{
this.ime = ime
this.kup = kup
this.kol = kol
}

var izd = new Array();

izd[++count]=new izdelek (&quot;rang&quot;,0,2) (sample for 'izd')

--- 2. called file from 1. HTML file -----------------------------------------------------
function recalc(form)
{ // function star

for (i=1;i<=3;i++)
{ // loop star
var quantity;
XXX: quantity=eval(&quot;form.quantity&quot;+i+&quot;.value&quot;);
parent.izd.kol = quantity;

} // loop end

} // function end


var htmout=&quot;&quot;;

for (i=1; i<=3; i++)

{ // start loop 2


htmout = '<div align=&quot;center&quot;><input type=&quot;text&quot; name=&quot;quantity' + i + '&quot; size=&quot;3&quot; maxlength=&quot;3&quot; value=&quot;' + parent.izd.kol + '&quot;>  <input type=&quot;button&quot; value=&quot;Recalc&quot; onClick=&quot;recalc(this.form)&quot;>'



if (parent.izd.kup==1)

{ // start condition

document.write(htmout);
document.write(&quot;<HTML><BODY><BR></BODY></HTML>&quot;)

} // end condition
} // end loop 2

---------------------------------------------------------------------------------------
Still ending up with: Object required !
quantity=eval(&quot;form.quantity&quot; + i).value <-- doesn't work as I still don't have object ????!!

 
did you try to alert the names of all elements ?
and do you get &quot;quantityi&quot; for each one ?
if yes, then correct this line : htmout = '<div align=&quot; .....

if no, then ... WHERE ARE YOUR FORM TAGS ? <form> and </form> ?
do &quot;view source&quot; on the GENERATED page and read the code ... and correct these lines :
document.write(htmout); document.write(&quot;<HTML><BODY><BR></BODY></HTML>&quot;)
 
:) yes, (looking for soap to wash my tongue). I knew I left out something fundamental: <form> tag
BUT :(
now I add it and I assigned alert to onClick command and names of form turned out fine (quantity1, quantity2), values are fine, if I change the field and click button alert displays new values. Everything is almost fine EXCEPT when I change onClick event back to call my function nothing happens. No errors, nothing, nada :(
even if I put in something like
function recalc(form)
{ alert (&quot;hello&quot;) }
still there is nothing !

Please, some clues on this one (and I'll be gone looking for ash) !
 
you mean, you have
---
<script language = javascript>
function recalc(..
...
</script>
</head>
<body>
<form>
<input ...>
<input type=button value=&quot;recalc&quot; onclick=&quot;recalc(this.form)&quot;>
</form>
</body></html>
---
and when you click on the button, nothing fires ? not even an error ??
(this is pretty stupid, but it often happens to me : check if the called function is correctly spelled)
(perform any check on the GENERATED page)

 
No, I have this:

<script language = javascript>
function recalc(..
...

htmstr= <form .......onClick... /form>

then a loop (i), variables(look above), condition and in this {

write.document (htmstr)

!!!! How could I accees the value of quantity from here ? !!!!?

}

</script>
</head>
<body>
.......


Problem is: when I didn't have <form> included in 'htmstr' the function recalc (onClick=recalc()) was called and it produced error (object required). Now I add <form> tags to the string (htmstr). And nada, nothing is called no errors if I click on button, nothing, function is where it always has been - no changes there (I checked again if I forgot maybe some {} - but then on the other side I didn't touch this function). If I replace onClick=alert(form.elemetns....) to check if now I have objects, everything turns out nice, correct values are displayed ... !
I can see that I'm missing some real important knowledge on how to incorporate (and evaluate) variables in JS. If I wanted to substitute OnClick with onChange=parent.izd.kol=quantity.value I end up with (parent.izd is not and object) error. And outside the 'htmstr' I can't access values of quantity no more (even If I try to access it with name of form, name.quantity)

 
------------------------------------------------------------<html>
<head>
<title>js</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-2&quot;>
<script language=&quot;JavaScript&quot;>
<!--

function izdelek(ime,kup,kol) // This is not normally here
{ // but I add it, so that this script could be run
this.ime = ime
this.kup = kup
this.kol = kol
}

var izd = new Array();
izd[1]=new izdelek(&quot;ekstra&quot;,1,9) // I preset the kup flag to 1 for this purpose //
izd[2]=new izdelek (&quot;rang&quot;,0,2) // you can flag/unflag yourself! //
izd[3]=new izdelek(&quot;agres&quot;,0,4)

function recalc(form)
{
alert (&quot;I'm here&quot;);

for (i=1;i<=3; i++)
{
var quantity;
quantity = eval(&quot;form.quantity&quot; + i + &quot;.value&quot;);
alert (quantity);

}
}

var htmout=&quot;&quot;;

for (i=1; i<=3; i++)

{

htmout = '<div align=&quot;center&quot;><form><input type=&quot;text&quot; name=&quot;quantity' + i + '&quot; size=&quot;3&quot; maxlength=&quot;3&quot; value=&quot;' + parent.izd.kol + '&quot;>&nbsp; <input type=&quot;button&quot; value=&quot;Recalc&quot; onClick=&quot;recalc(this.form)&quot;></form></div>'

if (izd.kup==1)

{
document.write(htmout);

}
}
-->

</script>
</head>

<body bgcolor=&quot;#FFFFFF&quot; topmargin=&quot;3&quot;>
<div align=&quot;center&quot;><font face=&quot;Verdana, Arial, Helvetica, sans-serif&quot; size=&quot;2&quot; color=&quot;#000000&quot;>
<div align=&quot;center&quot;> </div>
</body>
</html>
----------------------------------------------------------------------------------------------


Ok here is the whole program, just save it and run it :) ! The point is: How to assign the value from input form (type=text, ...)
to variable: izd.kol !
 
i can't save it and run it - and that's 3 or 4 times you post the exact same piece of code
i was asking you to do &quot;view source&quot; on the GENERATED page, copy it and past it here so that i can check if the form / fom elements / scripting elements are in correct order (sounds like they won't)
 
<html>
<head>
<title>js</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-2&quot;>
<script language=&quot;JavaScript&quot;>
<!--

function izdelek(ime,kup,kol)
{
this.ime = ime
this.kup = kup
this.kol = kol
}

var izd = new Array();
izd[1]=new izdelek(&quot;ekstra&quot;,1,9)
izd[2]=new izdelek (&quot;rang&quot;,0,2)
izd[3]=new izdelek(&quot;agres&quot;,0,4)

function recalc(form)
{
alert (&quot;I'm here&quot;);

for (i=1;i<=3; i++)
{
var quantity;
quantity = eval(&quot;form.quantity&quot; + i + &quot;.value&quot;);
alert (quantity);

}
}

var htmout=&quot;&quot;;

for (i=1; i<=3; i++)

{

htmout = '<div align=&quot;center&quot;><form><input type=&quot;text&quot; name=&quot;quantity' + i + '&quot; size=&quot;3&quot; maxlength=&quot;3&quot; value=&quot;' + parent.izd.kol + '&quot;>  <input type=&quot;button&quot; value=&quot;Recalc&quot; onClick=&quot;recalc(this.form)&quot;></form></div>'

if (izd.kup==1)

{
document.write(htmout);

}
}
-->

</script>
</head>

<body bgcolor=&quot;#FFFFFF&quot; topmargin=&quot;3&quot;>
<div align=&quot;center&quot;><font face=&quot;Verdana, Arial, Helvetica, sans-serif&quot; size=&quot;2&quot; color=&quot;#000000&quot;>
<div align=&quot;center&quot;> </div>
</body>
</html>
 
forget it
what i meant is that when processed (generated) your page will look like this :
---
<div align=center><form><input type=&quot;text&quot; name=&quot;quantity1&quot; size=3 ......></form>
<div align=center><form><input type=&quot;text&quot; name=&quot;quantity2&quot; size=3 ......></form>
<div align=center><form><input type=&quot;text&quot; name=&quot;quantity3&quot; size=3 ......></form>
----
and that's all : no <script> tag, your <fonts> are lost .... see ? NO MORE SCRIPT ... that's why it can't call it ...

because document.write REPLACES the text
it doesn't add or whatever

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top