Solved

How to make drop down list's list items visible/invisible using client side Javascript

Posted on 2009-07-05
3
1,050 Views
Last Modified: 2013-11-05
Hi there

I have to adjacent dropdown lists and what I want to happen is that if the user selects an item from the first, the items in the second will appear visible/invisible to the user depending on whether thet match the location the user selected in the first.

My Javascript is attached and executes without error but when but once I've selected my first drop down item, all items in the second are still visible even though the script should have made some invisible.

Code is attached.  Any help much aprreciated
Thanks Sam
function ShowAssociatedSchemes(principalMembers, schemesClientId) {

        var schemes;

        var value, locations, searchLocation

        if (principalMembers == null)

        {

            alert("Principal members drop down is null");

            return;

        }     

        

        schemes = document.getElementById(schemesClientId);

        

        if(schemes == null)

        {

            alert("Schemes drop down is null");

            return;

        }

                    

        for (var i = 0; i < schemes.options.length; i++) {

            if (principalMembers.value == "" || schemes.options[i].value == "") {

                alert("making option visible");

                schemes.options[i].style = "inline";

            }

            else {

                value = schemes.options[i].value;

                locations = value.substring(21)

                searchLocation = principalMembers.value.substring(0, 6);

                if (locations.indexOf(searchLocation) != -1) {

                    alert("making option visible");

                    schemes.options[i].style.display = "inline";

                }

                else {

                    alert("making option invisible");

                    schemes.options[i].style.display = "none";

                }     

            }        

        }

    }

Open in new window

0
Comment
Question by:Samm1502
3 Comments
 
LVL 10

Accepted Solution

by:
Bane83 earned 500 total points
ID: 24780534
Unfortunately, dropdowns don't allow for many CSS changes.  You can do some very basic things like font changes, but you can't apply visibility CSS to specific items.

The way around this is to place your dropdown items in an array and add/remove items to your second dropdown from this array based on your criteria.
0
 
LVL 14

Expert Comment

by:profya
ID: 24780538
Line 21:  schemes.options[i].style = "inline";
it should be:
 schemes.options[i].style.display = "inline";

Generally you can combine "visibility" and "display" to hide the option for example:
.style="visibility:hidden; display:inline;"
.style="visibility:visibile; display:none;"
0
 

Author Closing Comment

by:Samm1502
ID: 31599898
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now