Solved

Add items to select box dynamically using VBScript

Posted on 2004-03-20
4
3,186 Views
Last Modified: 2012-05-04
In Feb of this year.  Timbo87 gave a very short but very effective solution for adding items to a select box using JavaScript:

selectBox.length++
selectBox[selectBox.length-1].text = "new item"

I need to know what the equivalent VBScript code would be.  Or, if there really isn't an equivalant code using VBScript, perhaps someone can tell  me if there is a way to easily pass variables created/assigned  in VBScript  to JavaScript.
 
0
Comment
Question by:acaraciolo
  • 2
4 Comments
 
LVL 10

Expert Comment

by:D_M_D
Comment Utility
I got it to add a space for the item, but just not the text.  I need to look somemore.

<html>
<head>
<script language="vbscript">

Function AddItem(form)
dim itemSel, intItems
      
    itemSel = form.newitem.value
    intItems = form.sel1.length
    form.sel1.length = intItems + 1
   

end function

</script>
</head>
<body>
<form name="frmAdd">
<input type="text" value="" name="newitem">
<input type="button" name="abtn" value="add" Onclick="AddItem(frmAdd)">
<br>
<select name="sel1">
<option value="">Value1</option>
<option value="">Value2</option>
<option value="">Value3</option>
</select>
</form>
</body>
</html>

--------
D_M_D
0
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 500 total points
Comment Utility

This should help you....

<form name="test">

<select name="state" size="1">
  <option value="" selected></option>
  <option value="v1">v1</option>
  <option value="v2">v2</option>
  <option value="v3">v3</option>
</select>

<SCRIPT language = "VBScript">
    function additem()
      Set objOpt = document.CreateElement("OPTION")
      objOpt.Value = 1                        
      objOpt.Text = "v4"
      document.test.state.add objOpt
      Set objOpt = Nothing
    end function
</SCRIPT>

<input type=button value=add onclick="additem()">

</form>
0
 
LVL 12

Expert Comment

by:venkateshwarr
Comment Utility

In your case, it should be..

    Set objOpt = document.CreateElement("OPTION")
    objOpt.Value =  "new item"
    objOpt.Text =  "new item"
    selectBox.add objOpt

venkat
0
 

Author Comment

by:acaraciolo
Comment Utility
Great job venkateshwarr, that was exactly what I needed....  Thanks
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

763 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

13 Experts available now in Live!

Get 1:1 Help Now