Solved

Javascript Array iteration and removal

Posted on 2006-10-30
4
470 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
ID: 17833894
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 17834409
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 17834684
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
ID: 17837081
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

932 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

11 Experts available now in Live!

Get 1:1 Help Now