Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post

    Need help on return value for function..

    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:

    Code:
    <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~

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    Yea.. Ur right. It's not.. document.forms.test.blah+x1 is the real thing. yet it still returns "NaN"

  • #4
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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].

  • #5
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    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.

  • #6
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Just as I wrote above, use document.forms.test['blah'+x1] instead of document.forms.test.blah+x1.

  • #7
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    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:

    Code:
    <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~

  • #8
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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
    Code:
    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.

  • Users who have thanked venegal for this post:

    ShadowIce (05-17-2009)

  • #9
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post
    it works now. thank you very much for all of your help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •