Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onChange and onSelect in DropDownList

I have a DropDownList that asks the user if he want to join the club:

Do you want to join the club
Yes
No

Under this list there is another list that is set to disabled as a default. This list has the departments of the club. This list will not be enabled until the user chooses Yes.

I built the following code but the problem that I couldn't solve is let's assume the user chooses Yes then he changes his decision so he will choose No again. In this case the list of the departments still enabled. I want it to be disabled when he chooses No again.

<html>
    <head>
        <script type="text/javascript">
            function disable()
            {
                document.getElementById("mySelect1").disabled=true;
            }
            function enable()
            {
                document.getElementById("mySelect1").disabled=false;
            }
        </script>
    </head>
    <body>
        <form>
            <select id="mySelect" onChange="enable();">
                <option onSelect="disable();">No</option>
                <option onSelect="enable();">Yes</option>
            </select>
            <select id="mySelect1" disabled="disabled" >
                <option>Dep1</option>
                <option>Dep2</option>
                <option>Dep3</option>
                <option>Dep4</option>
            </select>
        </form>
    </body>
</html>

I thought that onSelect="disable();" would solve the problem but it still doesn't work.

Thanks

like image 728
Nasser Avatar asked Mar 29 '12 23:03

Nasser


People also ask

Can I use Onchange in select tag?

The onChange attribute of <select> lets you dynamically change something on screen, without reloading the page. For example, your can use it to display a particular image, depending on the user's selection.

How do I use Onchange in dropdown list in react JS?

To handle the onChange event on a select element in React: Set the onChange prop on the select element. Keep the value of the selected option in a state variable. Every time the user changes the selected option, update the state variable.

How do you write a single Onchange event for multiple dropdowns?

How to get the values from multiple dropdowns and then trigger a single onchange event. Use a standard jQuery selector. Get all select values. Get the selects with the my-select class.


3 Answers

Simply & Easy : JavaScript code :

function JoinedOrNot(){
    var cat = document.getElementById("mySelect");
    if(cat.value == "yes"){
        document.getElementById("mySelect1").disabled = false;
    }else{
        document.getElementById("mySelect1").disabled = true;
    }
}

just add in this line [onChange="JoinedOrNot()"] : <select id="mySelect" onchange="JoinedOrNot()">

it's work fine ;)

like image 53
Yassine Sedrani Avatar answered Oct 16 '22 12:10

Yassine Sedrani


I'd do it like this jsFiddle example.

JavaScript:

function check(elem) {
    document.getElementById('mySelect1').disabled = !elem.selectedIndex;
}

HTML:

<form>
    <select id="mySelect" onChange="check(this);">
        <option>No</option>
        <option>Yes</option>
    </select>

    <select id="mySelect1" disabled="disabled" >
        <option>Dep1</option>
        <option>Dep2</option>
        <option>Dep3</option>
        <option>Dep4</option>
    </select>
</form>
like image 17
j08691 Avatar answered Oct 16 '22 10:10

j08691


I bet the onchange is getting fired after the onselect, essentially re-enabling the select.

I'd recommend you implement only the onchange, inspect which option has been selected, and enable or disabled based on that.

To get the value of the selected option use:

document.getElementById("mySelect").options[document.getElementById("mySelect").selectedIndex].value

Which will yield .. nothing since you haven't specified a value for each option .. :(

<select id="mySelect" onChange="enable();">
<option onSelect="disable();" value="no">No</option>
<option onSelect="enable();" value="yes">Yes</option>
</select>

Now it will yield "yes" or "no"

like image 4
Halcyon Avatar answered Oct 16 '22 11:10

Halcyon