Attempting to disable text area when a specific option is selected

I have the following code:

	<select id="order_type" name="order_type">	
		<option value="" selected>- Select One -</option>
		<option value="pick-up">Pick-up</option>
		<option value="delivery">Delivery</option>
	</select>

	<label for="txt_address">Address</label> <br>
	<input id="txt_address" type="text" size="50" placeholder="123 Fake Street" style="text-align:center;"> <br>

I’m attempting to disable “txt_address” if the option “pick_up” is selected. I tried this piece of javascript:

if (document.getElementById(“dropdown”).value==“option1”)
{
document.getElementById(“textarea”).disabled = true
}

but it doesn’t seem to be working.
If anyone has any information in how it could be implemented to work that would be great.

It’s probably just a typo in your post, but…

if (document.getElementById("dropdown").value=="option1")

try changing “option1” in “pick-up” ?

Oh sorry, the section of JavaScript I added was from an email just to use as an example. I have used that same piece of Javascript, with all the appropriate amendments to the actual HTML I used.

Hi @POOLEYAWESOME, apart from the typos, you’d have to handle this in a change event listener – otherwise the check only runs once when the script gets evaluated, but doesn’t react to changes later on. Like

var textarea = document.getElementById('txt_address');
var select = document.getElementById('order_type');

select.addEventListener('change', function() {

  if (this.value === 'pick-up') {
    textarea.disabled = true;
  } else {
    textarea.disabled = false;
  }
});

Okay awesome. thanks.
So just to make something clear, is what you wrote before all to be added into the JavaScript validation code? Should there any changes to any of the HTML at all?

Not sure what you mean with validation code… just put it somewhere at the end of the body, so that those DOM elements are available when the the script runs. The script works with the HTML you posted above – here’s a fiddle.

1 Like

Thanks man, I’ll give it a try and let you know. Thanks for all the help.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.