0

Just wondering can anyone give me an example on how to disable a dropdown box, when an option in one isnt selected?

e.g

<td class="Item">State(Aus only):</td>
<td class="Data">
  <select class="TextBox" name="State" id="selectState">   
   <option>New South Wales</option>
   <option>Northern Territory</option>
   <option>Queensland</option>
  </select>
 </td>



 <td class="Item">Country:</td>
 <td class="Data">
   <select class="TextBox" name="Country" id="selectCountry">
    <option value="Australia" id="Australia" name="Australia">Australia</option>
    <option value="Braz">Brazil</option>
   <option value="China">China</option>
  </select>     
 </td>

   <script>
   function selected()
if(document.selectCountry.checked)      
{
document.getElementById("selectCountry.Australia").disabled = false;
    }   
else
    {   
document.getElementById("selectCountry.Australia").disabled = true;
}
  </script>

I want the State dropdown disabled with JAVASCRIPT unless Country is selected to be Australia.. can I get some help please?

I'm still kinda new to this whole Javascript coding so sorry if my code is bad but i hope you get my question

1 Answer 1

1

You need to fix you js code like this

function selected(){
 if(document.getElementById("selectCountry").value == "Australia") {
   document.getElementById("selectState").removeAttribute("disabled");
 }   
 else {   
   document.getElementById("selectState").setAttribute("disabled","disabled");
 }
}

Here is the fiddle for above code

Updated fiddle to call the function on page load if country is not by default "Australia"

Sign up to request clarification or add additional context in comments.

5 Comments

I tried to do this but it didn't work? I understand the code, but uh.. yeah
Is there something I might have to add to the select tag to call that function? or?
Yes you have to call your function on "onchange" event of country selectbox. Also you can call this function on page load to set default value. rest will be handled by "onchange" function call
Check the updated fiddle for calling the function on page load.
Thank you very much! Appreciate you taking time to help us newbies!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.