Re: Dependent dropdowns

From: Dave Anderson (GTSPXOESSGOQ_at_spammotel.com)
Date: 08/07/04


Date: Sat, 7 Aug 2004 11:49:47 -0500


"James Baker" wrote:
> Looking for a simple javascript solution to this and it's annoying me
> to no end.
>
> Long story short, I have two drop downs on a form (using VBScript as
> my server side language). Drop down 2 will have nothing in it, until
> you select an item from Drop down 1. I can't handle a submit here, I
> need to have it all be preloaded into Javascript (most likely arrays)
> that I can just access from the client side. The first drop down is
> "PropertyType" and the second is "LoanType". Essentially, each
> PropertyType has a list of loan types that are acceptable for it.
> Right now I keep getting lost in trying to design this array. What's
> the best javascript approach to a problem like this?

I find that one simple solution is to use multiple SELECT elements,
displaying only one at a time. To the user, it simply looks like one SELECT
object.

    <SELECT NAME="PropertyType" ONCHANGE="doStuff()"> ... </SELECT>

    <SELECT NAME="LoanType1" STYLE="display:none"> ... </SELECT>
    <SELECT NAME="LoanType2" STYLE="display:none"> ... </SELECT>
    ...
    <SELECT NAME="LoanTypeN" STYLE="display:none"> ... </SELECT>

    var dependentList =
        new Array(document.myform.LoanType1, ..., document.myform.LoanTypeN)

    function doStuff() {
        for (var i=0; i<dependentList.length; i++)
            dependentList[i].style.display = "none"

        { additional logic for deciding which element to reveal.
          To do so, set the .style.display propery to "block" }
    }

The advantages of this aproach are (a) it is easier to reliably build a set
of OPTION tags from arbitrary lists of data available on the server than to
build JS arrays, and (b) the demands on the client are smaller. This second
point only really matters when the secondary lists (and subsequently the
arrays) are long. Deconstruction and rebuilding of the dependent list can
bog down lesser machines.

-- 
Dave Anderson
Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.


Relevant Pages

  • Re: Using AJAX/JSON and performance issues with eval()
    ... evalevaluates its string argument as an ECMAScript ... JS arrays are implemented as objects, ... multiple HTML tables automatically in the background) and really want it ... Prototype.js was written by people who don't know javascript for people ...
    (comp.lang.javascript)
  • Re: Math.random
    ... But, as the seed is exposed, randoms can be ... Using just ordinary JavaScript ... arithmetic has to be done with arrays of digits. ... as one might reasonably have hoped, and also is a security leak. ...
    (comp.lang.javascript)
  • Re: Securing Link/URL Display
    ... Build a web part that emits a JavaScript array of the currently logged-in ... SharePoint University http://www.sharepointu.com ... > What about the lists themselves? ...
    (microsoft.public.sharepoint.windowsservices)
  • Re: JavaScript associative arrays not ordered?
    ... "In JavaScript an object is a mapping from property names ... If wikipedia wish to define an "associative array" as "a mapping from ... So much of what associative arrays ... Inevitably false expectations about javascript will not ...
    (comp.lang.javascript)
  • RE: Pre populate NewForm.aspx fields with values
    ... Ok I managed to prepopulate using javascript. ... the new form based on other fields in other lists and I'm having issuse ... And I even was able to display a preselected value of a connected lookup ...
    (microsoft.public.sharepoint.windowsservices)