...

View Full Version : Need help on return value for function..



ShadowIce
05-17-2009, 02:27 PM
Hi all. :) I have done some deep research on returning strings to an element id, and on for loops, but for some reason i cant seem to return the value of the text box i created. Instead, it returns a value of "NaN". The function that is returning "NaN" is "ztest()".

Here is the code as follows:


<script>
<!--Hide from old browsers
var x1 = 0;
var x2 = 0;
var num=4;
function ztest(){
if('document.test.forms.blah+x1.onClick'){
if(document.getElementById('boxes').innerHTML != null || document.getElementById('boxes').innerHTML != ""){
document.getElementById("zztest").innerHTML += document.test.forms.blah+x1.value;
}else{
alert('enter!');
}
}
}
function zsubmit(){
if('document.forms.test.zzsubmit.onClick'){
x2++;
}
alert(x2);
}
-->
</script>
<form name="test">
<p align="Center">
<script>
<!--Hide from old browsers
function addbox(){
if('document.forms.test.add.onClick'){
x1++;
}
document.getElementById('boxes').innerHTML += 'Name '+x1+': <input type="text" onClick="ztest();" name="blah'+x1+'"><br>n';
}
document.write('<span id="zztest" style="background-Color: #44CFFC; width: 400px; height: 50px;"></span><script>for(var x=1;x<=2;x++){document.write('<br>');}</script>n');
document.write('<input type="button" name="add" value="Add box" onClick="addbox();">n');
document.write('<input type="button" name="zzsubmit" onClick="zsubmit();" value="Submit"><br>n');
document.write('<a href="javascript:window.opener=false, window.close('-1');">Exit</a><br>n');
document.write('<span id="boxes" style="background-Color: #44CFFC; width: 400px; height: 25px;"></span><br>n');
-->
</script>
</p>
</form>

I simply need the text box to return the value of the box to the element.

thanks!

~SI~

venegal
05-17-2009, 03:05 PM
document.test.forms.blah doesn't sound like it's a real thing.

And document.writing document.write commands is a bit silly, especially when it's all static content, which you could do with vanilla HTML just as well.

ShadowIce
05-17-2009, 03:20 PM
Yea.. Ur right. It's not.. document.forms.test.blah+x1 is the real thing. yet it still returns "NaN"

venegal
05-17-2009, 03:42 PM
There are lots of problems here, but this one is probably the one you are caring about the most at the moment:

document.forms.test.blah+x1 is not a real thing. You are adding x1 to document.forms.test.blah which won't work, because blah is not a property of test, and it's not even what you are really trying to accomplish.

What you really want to use is document.forms.test['blah'+x1].

ShadowIce
05-17-2009, 04:02 PM
Then what would you do to fix this? I learn by example. i need "name=blah0", "name=blah1", etc.. as u hit a button and it adds another text box.

venegal
05-17-2009, 04:16 PM
Just as I wrote above, use document.forms.test['blah'+x1] instead of document.forms.test.blah+x1.

ShadowIce
05-17-2009, 06:46 PM
Ok. thanks. that worked :) Now there's a few more problems that I cant figure out..

When I enter something into the box then hit the submit button, it does add the "var a = new Array("a");", but it shows on the webpage, and not in document.getElementById("test").innerHTML. and when I click submit again, it keeps adding "var a = new Array("a");" "var a = new Array("a");" "var a = new Array("a");" instead of "var a = new Array("a","a");".

Here is the code box:


<script>
<!--Hide from old browsers

var x1=0;
var num=4;

function ztest(){

if('document.forms.test.zzsubmit.onClick'){

if (document.getElementById('boxes').innerHTML != null || document.getElementById('boxes').innerHTML != "") {

zname();

} else {

alert('enter!');

}
}
}

-->
</script>

<form name="test">
<p align="Center">

function zname(){

var vname = new Array("a");
var comma = '","';

var a = new Array(document.forms.test['blah'+x1].value);

var str = "";

str += 'var '+vname[0]+' = new Array("';

str += a.join(comma);

str += '");';

document.getElementById("test").innerHTML += str;

}
</script>

<script>
<!--Hide from old browsers
function addbox(){

if ('document.forms.test.add.onClick') {
x1++;
}
document.getElementById('boxes').innerHTML += 'Name '+x1+': <input type="text" name="blah'+x1+'"><br>\n';
}

document.write('<span id="zztest" style="background-Color: #44CFFC; width: 400px; height: 50px;"></span><script>for(var x=1;x<=2;x++){document.write(\'<br>\');}</script>\n');

document.write('<input type="button" name="add" value="Add box" onClick="addbox();">\n');

document.write('<input type="button" name="zzsubmit" onClick="ztest();" value="Submit"><br>\n');

document.write('<a href="javascript:window.opener=false, window.close(\"-1\");">Exit</a><br>\n');

document.write('<span id="boxes" style="background-Color: #44CFFC; width: 400px; height: 25px;"></span><br>\n');

-->
</script>

</p>
</form>

Thanks for all the help :)

~SI~

venegal
05-17-2009, 07:37 PM
First of all, please post the actual code you are working with. I had to change quite a few things in your snippet to get it to work at all.

Secondly, zname does not do at all what you want it to do. You are not populating the array "a" with all the values, only with the last one, and you are not changing the innerHTML of the element "test" (which does not even exist in your snippet), instead you are concatenating to it, so it's pretty obvious why it does what it does.

You could try it with

function zname(){
var vname = new Array("a");
var comma = '","';
var i = 1;
var a = [];
var str = "";

while (typeof(document.forms.test['blah' + i]) != "undefined") {
a.push(document.forms.test['blah' + i++].value);
}

str += 'var '+vname[0]+' = new Array("';
str += a.join(comma);
str += '");';

document.getElementById("test").innerHTML = str;
}

but I strongly suggest you read up on the basics of javascript and best practices before trying to do cobble together a script you are going to need help with every step of the way.

ShadowIce
05-17-2009, 11:59 PM
it works now. thank you very much for all of your help! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum