0

I want to make this textbox enabled when I choose a specific value from a select drop-down, and disabled when I select any other option.

This is my html:

<select name="status" id ="combo">
      <option value="_">Please choose..</option>
        <option  value="Pending">Pending</option>
        <option  value="Process">Process</option>
        <option  value="Delivered" >Delivered</option>
      </select>
 <tr valign="baseline">
 <td nowrap="nowrap" align="right" class="postage" >Postage:</td>
 <input type="text" name="postage" id="textbox" >
4
  • You need javascript or javascript library named jquery to do it :-) I can write you the script. Commented Apr 20, 2015 at 15:27
  • 6
    Can you show what you've attempted so far? P.S. This is not valid HTML. You can't have a <select> as a sibling of a <tr> and you can't have an <input> as a child of a <tr> Commented Apr 20, 2015 at 15:27
  • @Redrif Adding jQuery to a project for the simple task at hand is an overkill. It is bad advise to tell people "add jQuery" as a solution to every problem. jQuery is great, I use it on my projects when it is necessary, but adding it should be a decision you make based on the entire project and target deployment platforms. We simply don't have enough information here to make that kind of suggestion. jQuery is thousands of lines of code, adding it to do one basic task is like using a chainsaw to cut a birthday cake. Commented Apr 20, 2015 at 15:29
  • @Redrif I stand by the advice, though on this particular question, jQuery should have been added to the tags. It was removed from the title by someone else, and they didn't add the tag when they did so. OP already, apparently, has jQuery :) Commented Apr 20, 2015 at 15:51

3 Answers 3

3

Try this:

var select_element = document.getElementById( "combo" );
var selected = select_element.options[ select_element.selectedIndex ].value

if(selected == "Delivered"){
  document.getElementById("textbox").disabled = false;
}else{
  document.getElementById("textbox").disabled = true;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I saw the original title with jQuery but someone edited it. Anyway just incase you wondered how it's done in jQuery here it is:

 $(document).ready(function () {
        var $input = $('input[name=postage]');
        $input.attr('disabled', 'disabled');
        $('select[name=status]').on('change', function () {
            $input.attr('disabled', $(this).val() != "Delivered");
        });
    });

demo: http://jsfiddle.net/c4rks52r/

3 Comments

As noted by Chris Baker, user has not indicated having access to jQuery. Also, I believe you are supposed to use prop now as opposed to attr.
@ScottSmith Actually, I just saw the edit history, someone DID take the word "jQuery" out of the title, without adding the tag. If marcel wants to undelete his answer, I'll retract my downvote.
Ah nice catch, did not see that.
0

This would be how I would do this using jQuery (demo here):

$(document).on('change', '#combo', function(){
    var shouldEnable = $(this).val() !== 'Delivered';
    $('#textbox').prop('disabled', shouldEnable);
});

Side note, did you ever say specifically which value being selected is the trigger for enabling? I just went with Delivered as everyone else went that route. Maybe I missed where you said that explicitly. Also, based on your description on wanting to enable it only when a specific value is selected, I assume you would want it disabled initially when the page loads. My demo has that in place.

Comments

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.