0

i want to hide and show my text box according to drop down select value. if user select drop down value "other" then i want show my text box and hide drop down list and he can select any value except other my text box is hide plz help me and give me sample code my code is here

 <script type="text/javascript">
  $( function () {

 if($("#subject").val()==="Other") {
     $("#Sub").show();
  $("#subject").hide();

  }
  else $("#Sub").hide(); 
    });

  <tr> <td style="width: 470px"><%:Html.DropDownList("subject",ViewData["sub"] as SelectList) %> 

      <input type ="text"  name="subject" id="Sub"style="width: 250px" /></td></tr>

i populated drop down list from data base

2 Answers 2

6

To do this you must attach an event on "change" event of the select (and hide the text input first). try this:

$("#Sub").hide();
$("#subject").change(function(){
    if ($(this).val() === "Other"){
        $("#Sub").show(); 
        $("#subject").hide();
    }else{
        $("#Sub").hide();
    }
});
Sign up to request clarification or add additional context in comments.

7 Comments

try u r code i select second value the my text box also hide without selecting other
You wanted to hide the selct and show the textbox when you select "other" and hide the textbox when you select any vlue that is not other. that is what i understood. Post your generated html code so that i can help you better
when i select any value except "other" i want hide my text box when i select the value "other" then i want hide my select box
Look at the fiddle here: jsfiddle.net/YjtD9 if you textbox hides too when you select "other" you might have some different problem. try to show us the generated html
<script type="text/javascript"> $("#Sub").hide(); $("#subject").change(function () { if ($(this).val() === "Other") { $("#Sub").show(); $("#subject").hide(); } else { $("#Sub").hide(); } }); <td style="width: 470px"><%:Html.DropDownList("subject", ViewData["sub"] as SelectList)%> <input type ="text" name="subject" id="Sub"style="width: 250px" /></td></tr>here is my code plz help me i check out u r link same i want
|
1

try this

  <tr> <td style="width: 470px"><%:Html.DropDownList("subject",ViewData["sub"] as SelectList,new {onchange="ShowHideTextBox()"}) %> 

  <input type ="text"  name="subject" id="Sub" style="width: 250px" /></td></tr>

javascript

  <script type="text/javascript">
function ShowHideTextBox() {

    if($("#subject").val().toUpperCase()=="OTHER") {
         $("#Sub").show();
         $("#subject").hide();
    }
    else
    {
       $("#Sub").hide(); 
    } 
}
</script>

4 Comments

this code is not working when i select value "other " the text box also hide
this because I compare with Other not other so I edited my answer to convert the value of dropdown to upper case and compare it with OTHER
thanks for replay i try it but it is not working i use asp mvc view plz help me
try to use newer version of jquery 1.6.2

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.