Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-28-2012, 10:19 AM   PM User | #1
sanidhya09
New to the CF scene

 
Join Date: Sep 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
sanidhya09 is an unknown quantity at this point
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>
sanidhya09 is offline   Reply With Quote
Old 09-28-2012, 10:25 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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?
devnull69 is offline   Reply With Quote
Old 09-28-2012, 11:01 AM   PM User | #3
sanidhya09
New to the CF scene

 
Join Date: Sep 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
sanidhya09 is an unknown quantity at this point
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.
sanidhya09 is offline   Reply With Quote
Old 09-28-2012, 11:52 AM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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;
devnull69 is offline   Reply With Quote
Old 09-28-2012, 02:43 PM   PM User | #5
sanidhya09
New to the CF scene

 
Join Date: Sep 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
sanidhya09 is an unknown quantity at this point
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>
sanidhya09 is offline   Reply With Quote
Old 09-29-2012, 05:53 AM   PM User | #6
green_meep
New Coder

 
Join Date: Sep 2012
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
green_meep is an unknown quantity at this point
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 05:56 AM..
green_meep is offline   Reply With Quote
Old 09-29-2012, 02:32 PM   PM User | #7
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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>
xelawho is offline   Reply With Quote
Reply

Bookmarks

Tags
array value in input text

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:25 PM.


Advertisement
Log in to turn off these ads.