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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to get array value in input box

    Code:
    <%-- 
        Document   : index
        Created on : 28 Sep, 2012, 11:41:16 AM
        Author     : Sanidhya09
    --%>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
       </head>
        <body>
            <form method="post">
    <script>
    function Gurgaon()
    {
    document.getElementById("demo").value=sector2();
    function sector2()
    {
        var b=new Array;
        b[0]="sector4";
        b[1]="sector5";
        b[2]="sector6";
        var j;
        for(j=0;j<=((b.length)-1);j++)
        {
        return(b[j]);
        }
    }}
    </script> 
    
                     <select name="drop" size="1">
                    <option value="none" selected="selected" ></option>
                 <option value="Gurgaon" onClick="Gurgaon()">Gurgaon</option>
                      </select>
                 <br/><br/>
                       <script></script>
                 <input type="text" name="demo" id="demo" />
            </form>
        </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    You cannot return values from inside a loop. Ok, you can, but it will stop the loop and return only the first value b[0] of the array.

    What EXACTLY do you want to achieve?

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    You cannot return values from inside a loop. Ok, you can, but it will stop the loop and return only the first value b[0] of the array.

    What EXACTLY do you want to achieve?
    I want all the values in input box. pls help, as i am very new to javascript.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    This is not EXACTLY what you want, is it?
    Code:
    var b=[];
    b[0]="sector4";
    b[1]="sector5";
    b[2]="sector6";
    var result = "";
    for(var j=0;j<b.length;j++)
    {
       result += b[j];
    }
    document.getElementById("demo").value = result;

  • #5
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    This is not EXACTLY what you want, is it?
    Code:
    var b=[];
    b[0]="sector4";
    b[1]="sector5";
    b[2]="sector6";
    var result = "";
    for(var j=0;j<b.length;j++)
    {
       result += b[j];
    }
    document.getElementById("demo").value = result;
    function Noida()
    {
    var a=new Array;
    a[0]="sector1";
    a[1]="sector2";
    a[2]="sector3";
    var i;
    for(i=0;i<=((a.length)-1);i++){
    document.getElementById("demo").innerHTML=a[i];
    //document.write(a[i]);
    }
    }
    I want to print all the array value in the "same" document
    in <p id="demo"></p>

  • #6
    New Coder
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sanidhya09 View Post
    function Noida()
    {
    var a=new Array;
    a[0]="sector1";
    a[1]="sector2";
    a[2]="sector3";
    var i;
    for(i=0;i<=((a.length)-1);i++){
    document.getElementById("demo").innerHTML=a[i];
    //document.write(a[i]);
    }
    }
    I want to print all the array value in the "same" document
    in <p id="demo"></p>
    This doesn't work because you're just swapping out the innerHTML each time.

    I figured you'd want some spaces between your concatenations. Obviously you can just take out the space if you don't want/need it.

    EDIT: I just realized. devnull's and my posts do pretty much the exact same thing. Probably the reason it's not working (if that's the entirety of your HTML up there) is because you don't have a "<p id="demo"></p>" in the first place.

    Code:
    function Noida()
    {
        var a=new Array;
        var result="";
        a[0]="sector1";
        a[1]="sector2";
        a[2]="sector3";
        var i;
        for(i=0;i<=((a.length)-1);i++)
          result=result + a[i] + " ";
        document.getElementById("demo").innerHTML=result;
    }
    Last edited by green_meep; 09-29-2012 at 06:56 AM.

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,990
    Thanks
    56
    Thanked 557 Times in 554 Posts
    there is a text box with the id demo. if you want the results to end up there you need to change that line to:
    Code:
    document.getElementById("demo").value=result;
    but you should also look at how you are calling your function. Onclicks on options in a select box are notoriously unreliable. If that's all you want your code to do I would suggest making your select like this:

    Code:
    <select name="drop" size="1" onchange="Gurgaon()">
    <option value="none" selected="selected" ></option>
    <option value="Gurgaon">Gurgaon</option>
    </select>


  •  

    Tags for this Thread

    Posting Permissions

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