How can I add elements to SELECT with VBScript ?
Thanks !
Printable View
How can I add elements to SELECT with VBScript ?
Thanks !
You refering to HTML form tags or SQL queries???
Ok, my fault; I wasn't explicit enough.
It may sound dumb but just plain HTML, how can I add dynamically element to SELECT ?
Thanks
[2nd Attempt to Post]
What, are Josh, Monte, and me the only ones working this forum?
Nino, you can build an loop and write your options.
'Course, there are a dozen different ways I could've done that: not use an array, use Response.Write to cut down on the <% %>, yadda yaddaCode:<select name='mySelect'>
<%
for i = 0 to UBound(MyOptions, 1)
%>
<option value='<%=MyOptions(0, i)%>'><%=MyOptions(1, i)%></option>
<%
next 'i
%>
</select>
Thank you Ciberthug, but that's not what i was looking for;
I figured out how to do that in asp, but if the users decides to add an element to existing options I enumerate in asp ?
This is in a form to add a new record to our db.
It's working well, but since there are no combobox in HTML, i choose to use textboxes in conjunction with listbox (synchronized). needless to say, my boss didn't like it as much as I did lol.
So he asked me to redesign the form w/o textboxes and just buttons instead where the user should click and then an inputbox would ask them what they would like to add...
I just need the part where in vb i would have done this :
cboItems.additem "MyNewItem"
cboItems.Text = "MyNewItem"
Thanks !
Pretty much the same in VBScript except the [] are () and you don't need the ; at the end of the line.
I think for VBScript it is something like this:
Code:<script language=VBScript>
Sub addNew
Dim objOption
Set objOption = document.createElement("OPTION");
objOption.innerText = 'Whatever'
objOption.value = '5'
myForm.mySelect.options.add(objOption)
Set objOption = Nothing
End Sub
</script>
:confused:
Ok thanks for the reply, but have you tried your script ?
It's not working.
I can't mix Vbscript & javascript so I'll find another way I guess... !
Thanks for the reply again !
Who's script, mine or Monte's? I told you mine was off.
But what do you mean you can't mix VBScript and JavaScript? They can't be in the same script tag, but... I didn't realize they couldn't be on the same page.
I would check on that, but I don't ever use VBScirpt for client-side, so I don't care. :D I use VBScript for the ASP server-side and JavaScript for the client-side.
Monte's script.
I'm sorry for the misunderstanding; when I said I couldn't mix VBScript and javascript, I meant that I thought Vbscript couldn't call a function in javascript... Am I wrong ?
Anyway, I found a solution, I used another way w/o adding options !
Thanks anyway guys !
VBWorld is the best place on the web !
Oh, no, you are right. VBScript couldn't call a JavaScript function and vice versa. They would pretty much run as two seperate programs with their own memory space ontop of the page. They could access the same page elements, though, but not each other.
I was doing that from the top (or maybe the bottom?) of my head.. didn't bother to test it... should have gotten you close though....
like:
Code:<script language=VBScript>
Sub addNew
Dim objOption
Set objOption = document.createElement("OPTION");
myForm.mySelect.options.add(objOption)
//Assuming your form is named myForm and your select
//is named mySelect
objOption.innerText = 'Whatever'
objOption.value = '5'
Set objOption = Nothing
End Sub
Hehe monte's I'm not that dumb, Yet ( :D )
I modified your script but couldn't get it to work, plus you forgot the Sub() and left javascript's ";" ;)
Thanks anyway !