Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selectbox disabled or enabled by an option of an other selectbox

I try to create a relation/connexion beetwen 2 selectbox

<form id="creation" name="creation">
<select name="select01" id="select01">
     <option value="01">01</option>
     <option value="02">02</option>
     <option value="03">03</option>
</select>

<select name="select02" id="select02">
     <option value="aa">aa</option>
     <option value="bb">bb</option>
     <option value="cc">cc</option>
</select>
</form>

I want that my selectbox select02 be disabled at the begening, and when i click on value02 or value03, we enable it, and when we click on value01, disable it.

There is my code js for the moment. I try to adapt it from an other function (radiobutton enabling a selectbox) but it doesn't seem to work. My list is disable at the beging, but the onclick is not working ...

Some body can help me?

var oui = document.creation.select01[0];

document.getElementById("select02").disabled=true;
oui.onclick = function() {
document.getElementById("select02").disabled=false;
}

Ok, so NOW, i'm with this code:

<script>
var oui = document.select01;
document.getElementById("select02" ).disabled=true;
oui.onchange = function(){
document.getElementById("select02" ).disabled=false;
}
</script>

I change the value of my selec01, then my select02 is now enable .. but how can I target only 2 options of my select01 ?

like image 420
Peter Avatar asked Sep 04 '12 18:09

Peter


People also ask

How to disable option in dropdown html?

We use <select> and <option> elements to create a drop-down list and use disabled attribute in <select> element to disable the drop-down list element. A disabled drop-down list is un-clickable and unusable. It is a boolean attribute.

How to select disabled option in jQuery?

Set the attribute disabled = "disabled" in the <option> element to make the custom select menu option disable.

How to disabled select in html?

The disabled attribute for <select> element in HTML is used to specify that the select element is disabled. A disabled drop-down list is un-clickable and unusable. It is a boolean attribute.


1 Answers

Try this:

<form id="creation" name="creation" onchange="handleSelect()">
<select name="select01" id="select01">
     <option value="01">01</option>
     <option value="02">02</option>
     <option value="03">03</option>
</select>

<select name="select02" id="select02" disabled>
     <option value="aa">aa</option>
     <option value="bb">bb</option>
     <option value="cc">cc</option>
</select>
</form>

And as a JS script:

 function handleSelect() {
     if (this.value == '01') {
         document.getElementById('select02').disabled = true;
     } else {
         document.getElementById('select02').disabled = false;
     }
 }

And a tip: explore jQuery, a very popular JS framework (jquery.com). You can achieve your task very simply by using it.

like image 101
cassi.lup Avatar answered Oct 29 '22 01:10

cassi.lup