Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  146] [ 7]  / answers: 1 / hits: 62270  / 12 Years ago, tue, september 4, 2012, 12:00:00

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 ?


More From » html

 Answers
73

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.


[#83249] Monday, September 3, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
billie

Total Points: 101
Total Questions: 114
Total Answers: 98

Location: Burundi
Member since Wed, Nov 25, 2020
4 Years ago
;