javascript - Populate input text box based on drop down select box in Jquery -


i have drop down select box , input text box. select box display categories , this:

<select id="category" name="category">    <option value="">please select...</option>    <option value="1">category-1</option>    <option value="2">category-2</option>    <option value="3">category-3</option>      <option value="4">other</option>  </select> 

input text box this:

<input type="text" id="othercategory" name="othercategory" value="" style="display: none;"> 

my question is. when user select "other" dropdown need populate input text.

i tried this:

$(document).ready(function() {      $('#category').change(function() {         var myvalue = $(this).val();         var mytext = $("#category :selected").text();          if (mytext != '' , mytext == "other") {            $("#othercategory").show();         }     }); }); 

but couldn't work. can tell how figure out.

note: dropdown select populating dynamically.

thank you.

you missing && in if condition. also, condition

mytext != '' redundant , not required.

and need hide input when selection changed.

$(document).ready(function () {      $('#category').on('change', function () {         var myvalue = $(this).val();         var mytext = $.trim($("#category :selected").text()).tolowercase(); // trim spaces , convert lowercase comparison          $("#othercategory").toggle(mytext === 'other');     }); }); 

demo: https://jsfiddle.net/tusharj/8ykfmtyt/1/


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -