Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Javascript Array iteration and removal

Posted on 2006-10-30
4
Medium Priority
?
498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
callrs earned 2000 total points
ID: 17833894
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one 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…

610 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