CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   DOM and JSON scripting (http://www.codingforums.com/forumdisplay.php?f=15)
-   -   How to get array value in input box (http://www.codingforums.com/showthread.php?t=274546)

sanidhya09 09-28-2012 10:19 AM

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>


devnull69 09-28-2012 10:25 AM

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?

sanidhya09 09-28-2012 11:01 AM

Quote:

Originally Posted by devnull69 (Post 1274402)
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.

devnull69 09-28-2012 11:52 AM

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;


sanidhya09 09-28-2012 02:43 PM

Quote:

Originally Posted by devnull69 (Post 1274417)
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>

green_meep 09-29-2012 05:53 AM

Quote:

Originally Posted by sanidhya09 (Post 1274472)
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;
}


xelawho 09-29-2012 02:32 PM

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>



All times are GMT +1. The time now is 09:29 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.