Solved

Javascript Array iteration and removal

Posted on 2006-10-30
4
468 Views
Last Modified: 2012-06-21
I want to create a Javascript Array using a form select box that the user would click and create said array.

The code below will show a functional selectbox which adds selections into a text area.

I cannot figure out how to iterate the array so that everytime the select box changes, an increasing value (0,1,2,3,4,5) gets configured for the Array's Identifier.
 
does concat() help with this?

Later ON, I'll want to allow the user to remove something from the array.

does slice() work with this?

Here is my code so far.

<Script>
function writeval()
{
var val;
var ct;
var valary = new Array();
val=document.grab.Choice.value;
document.grab.textarea1.value+=val + " ";
valary[0] = val;
ct = valary[0];
}
</script>

<table>
<form name=grab method=post action=next.php>
<Tr><Td valign=top>
<Select NAME=Choice onChange= "writeval();">
<OPTION VALUE = "Football">Football</OPTION>
<OPTION VALUE = "Baseball">Baseball</OPTION>
<OPTION VALUE = "Hockey">Hockey</OPTION>
</Select>
</td><td>
<textarea readonly name=textarea1 rows=5 cols=50></textarea></td></tr>

</form>
</table>


0
Comment
Question by:qdcsinc
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
callrs earned 500 total points
Comment Utility
0
 
LVL 23

Expert Comment

by:rama_krishna580
Comment Utility
0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
callrs, why are you pointing people to a site that requires registration?  Please put the answer here.

You are having problems because:
- every time you invoke writeval(), a new valary is created.

------------------------------------
<html>
<head>
<title> Grab </title>
<script>
  var valary = [];
  function writeval() {
    var sel  = document.getElementById( 'pick' );
    if ( sel.selectedIndex ) {
      var opts = sel.getElementsByTagName( 'option' );
      var val  = opts[ sel.selectedIndex ].value;
      valary[ valary.length ] = val;
      document.getElementById( 'out' ).value = valary.toString();
    }
  }
</script>
</head>
<body>

<form name='grab' method='post' action=''>
  <table>
    <tr>
      <td valign=top>
        <select id='pick' name='Choice' onChange='writeval();'>
          <option>Please make a selection</option>
          <option>Football</option>
          <option>Baseball</option>
          <option>Hockey</option>
        </select>
      </td>
      <td>
        <textarea id='out' readonly name='textarea1' rows='5' cols='50'></textarea>
      </td>
    </tr>
  </table>
</form>

</body>
</html>
0
 
LVL 30

Expert Comment

by:callrs
Comment Utility
Thanks for the A.

No registration needed. It's a super-fast file sharing site, and allows an instant demo vs. copy/paste/save/open.

Once satisfied, then final version can be pasted:

<script type="text/javascript">
var valary = new Array();
var I=0;
function writeval(obj){
var frm=obj.form;
var val;
var ct;
val=frm.Choice.value;
frm.textarea1.value+=I+" "+val + " ";
valary[I++] = val;
ct = val;
}
</script>

<table>
<form name=grab method=post action=next.php>
<Tr><Td valign=top>
<Select NAME=Choice onChange= "writeval(this);">
<OPTION VALUE = "Football">Football</OPTION>
<OPTION VALUE = "Baseball">Baseball</OPTION>
<OPTION VALUE = "Hockey">Hockey</OPTION>
</Select>
</td><td>
<textarea readonly name=textarea1 rows=5 cols=50></textarea></td></tr>

</form>
</table>
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now