Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 11th, 2005, 07:31 AM
Registered User
 
Join Date: Feb 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to preethi.c
Default regarding combo boxes using html and javascript

I want to use a combo box which dynamically accepts values from the user and the value should be added into the selection list of the combo box options using html and javascript.

preethi
 
Old February 11th, 2005, 12:23 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I tried many ways of doing this, and wound up having to manipulate the .outerHTML property of the drop-down. Changing the .innerHTML had no effect.
So let’s say you never add carriage returns between the last element and the ending </select>. That means you have 9 characters for the ending.
So you could store all of the outerHTML—minus the last 9 characters.
Add to that <option value="...">...</option> for each new entry, followed by + </select>.
 
Old February 11th, 2005, 11:25 PM
Registered User
 
Join Date: Feb 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to preethi.c
Default

i want to add the options in a combo box dynamically using HTML and Javascript please tell me how to extend the size of the array in the code in this case.

preethi
 
Old February 14th, 2005, 11:43 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Well, its not an array.

Let’s say your combo is:
Code:
   <select name="sel" id="sel">
       <option value="01" selected>2001</option>
       <option value="02"         >2002</option>
       <option value="03"         >2003</option>
   </select>
   and you want to add ‘2004’ on the end.
Further, let’s presume there are no CRs in the html, as I said before. (If you have spaces, CRs, tabs, whatever, I’ll let you do your own character counting...)

So then:
Code:
  function Add_04()
  {
    var ctl = document.all["sel"];           // Set a reference to the control
    var  s  = ctl.outerHTML;                 // Get its contents
    s =  s.substr(0, s.length - 9);          // Get rid of the </select> on the end
    s += "<option value="04">2004</option>"; // Add your new list item(s)
    s += "</select>";                        // Reconstruct the end of the html
    ctl.outerHTML = s;                       // Put the string into the control.
  }

Now your html would be:
Code:
   <select name="sel" id="sel">
       <option value="01" selected>2001</option>
       <option value="02"         >2002</option>
       <option value="03"         >2003</option>
   <option value="04">2004</option></select>
   (Note that because of the fairly primitive string handling, the new text went in right where the ‘</select>’ had been, and now ‘</select>’ follows immediately after the last item in the list, with no formatting. The browser won’t care...)
 
Old March 3rd, 2005, 04:29 AM
Registered User
 
Join Date: Mar 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Brian.

It works well for me.
But could this codw works in Mozilla browser?

Thanks in advance,
Nickel

 
Old March 3rd, 2005, 06:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

why not just use the DOM?
function Add04()
{
    var oOption = document.createElement("option");
    oOption.value = "04";
    oOption.innerHTML = "2004";

    var oSelect = document.getElementById("sel");
    oSelect.appendChild(oOption);
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
3 combo boxes ttkt Beginning PHP 0 July 2nd, 2005 02:20 PM
Using xsl to create HTML combo boxes rdavisiii XSLT 4 November 23rd, 2004 05:40 AM
Combo Boxes tjs206 VB Databases Basics 2 December 10th, 2003 05:20 PM
Combo Boxes tjs206 VS.NET 2002/2003 7 November 7th, 2003 04:45 PM
Combo Boxes Louisa Beginning VB 6 2 September 10th, 2003 09:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.