1

How can I make interactive HTML form, example :

<select name="command">
    <option value="send">Send</option>
    <option value="cancel">Cancel</option>
</select>

<!-- 
    I want the following text input hidden by default,
    but active only if option "Cancel" is selected
-->
<input type="text" name="cancel_reason" id="needreason">

I want the "cancel_reason" input field is hidden by default, and shown if dropdown-option "Cancel" is selected before, otherwise it should remain hidden.

1
  • well @meouw just googling around.. tried some 'onChange' event based on #id.. but still not working.. still working around Commented Nov 16, 2011 at 7:59

3 Answers 3

5
$('select[name=command]').change(function(){
    if($(this).val() == 'cancel')
    {
       $('#needreason').show();
    }
    else
    {
       $('#needreason').hide();
    }
});
Sign up to request clarification or add additional context in comments.

Comments

2

Have a look at this jsFiddle. Non jQuery

<select id="command" name="command" onchange="javascript:selectChanged()">
    <option value="send">Send</option>
    <option value="cancel">Cancel</option>
</select>

<!-- 
    I want the following text input hidden by default,
    but active only if option "Cancel" is selected
-->
<input type="text" id="needreason" name="cancel_reason" style="display:none">

<script>
    function selectChanged()
    {
        if (document.getElementById('command').value == "cancel")
            document.getElementById('needreason').style.display = 'block';
        else
            document.getElementById('needreason').style.display = 'none';
    }
</script>

2 Comments

Please post your answer here. Add a fiddle if you like, but don't force me to go there
just great @xbonez except for double id's there.. i changed one of the 'id' to 'name' and adjust the 'getElementById' to the latest 'id'.. works like a charm.. thanks a lot :-)
0

Use this javascript function

function show_text_box(value)
{
  if(value=='cancel')
  {
    $('#needreason').show();
  }
  else
  {
    $('#needreason').hide();
  }
}

Call this function on your select at onchange event

<select name="command" onchange="show_text_box(this.value);">

and set text box hide

<input style="display:none;" type="text" name="cancel_reason" id="needreason">

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.