Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - How to show/hide text box based on selected drop down

Tags:

jquery

Sorry if this is extremely obvious, but I have looked and looked for a solution but haven't found anything. I'm very new to jQuery, so even looking for what I want to do has been hard.

I have a page with a bunch of fields and drop down boxes that are populated from a database. So each drop down has the correct item (the one stored in the database) selected when the page loads. When that option is "Other" I want my Other text box to show up.

So, the question is; how do I show/hide that text box based on the selected drop down item when the page loads? Everything I have been able to find in my research has been related to the drop down menu changing. This works fine for one of the other pages I have. I don't want to have to reselect "Other" (triggering the change event).

Thanks

like image 666
navalhawkeye Avatar asked Jun 21 '11 15:06

navalhawkeye


2 Answers

$(function() {
    if($("#yourDropDown").val() == 0) //I'm supposing the "Other" option value is 0.
         $("#yourTextBox").hide();
});
like image 88
Diego Avatar answered Oct 04 '22 16:10

Diego


$("option").bind('click', function(){
    var selected = $(this).val();
    alert(selected+' selected');
    if (selected == '1') { /* do anything you want */ }
    if (selected == '2') { /* do anything you want */ }
    //etc ...
});
like image 25
genesis Avatar answered Oct 04 '22 17:10

genesis