Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery select change show/hide div event

I am trying to create a form which when the select element 'parcel' is selected it will show a div but when it is not selected I would like to hide the div. Here is my markup at the moment:

This is my HTML so far..

    <div class="row">    
    Type
        <select name="type" id="type" style="margin-left:57px; width:153px;">
                <option ame="l_letter" value="l_letter">Large Letter</option>
                <option name="letter" value="letter">Letter</option>
                <option name="parcel" value="parcel">Parcel</option>
        </select>                    
</div>

<div class="row" id="row_dim">
    Dimensions
        <input type="text" name="length" style="margin-left:12px;" class="dimension" placeholder="Length">
        <input type="text" name="width" class="dimension" placeholder="Width">
        <input type="text" name="height" class="dimension" placeholder="Height">
</div> 

This is my jQuery so far..

  $(function() {
    $('#type').change(function(){
        $('#row_dim').hide(); 
        $("select[@name='parcel']:checked").val() == 'parcel').show();   
    });
});
like image 509
user2716389 Avatar asked Sep 02 '13 11:09

user2716389


People also ask

How to show/hide Div on dropdown selected using jQuery?

In this tutorial, learn how to show/hide div on dropdown selected. The short answer is: use the jQuery show () and hide () to show hide div based on select option value in jQuery. But, before you perform this, you need to first hide all the div items using CSS display:none.

How to show or hide a DIV based on select option value?

You have selected option “Three” . There are three options given in the select box to select. You have to select the option to display the relevant div element. To show hide div based on select option value selected, the dynamic changes can handle by using some jQuery script as given in the example above.

How to show/hide event in jQuery?

Just add below JQuery code snippet in your javascript file and voila, you can now handler ‘show’/’hide’ events using .on () method similar to other implicit events. Once the code is in place, you can simply use .on () method as below to add custom handler to show/hide event to any element.

How do I show a Div in a drop down menu?

Show/Hide Div On Dropdown Selected Using jQuery. If you want to show/hide div on dropdown selected, use the jQuery show () and hide (). Before you perform show/hide div on dropdown selection, you need to hide them first using CSS display:none. The display of the div dynamically happen based on the click of the selected dropdown option.


4 Answers

Use following JQuery. Demo

$(function() {
    $('#row_dim').hide(); 
    $('#type').change(function(){
        if($('#type').val() == 'parcel') {
            $('#row_dim').show(); 
        } else {
            $('#row_dim').hide(); 
        } 
    });
});
like image 126
Yasitha Avatar answered Oct 21 '22 14:10

Yasitha


<script>  
$(document).ready(function(){
    $('#colorselector').on('change', function() {
      if ( this.value == 'red')
      {
        $("#divid").show();
      }
      else
      {
        $("#divid").hide();
      }
    });
});
</script>

Do like this for every value Also change the values... as per your parameters

like image 37
saurabh yadav Avatar answered Oct 21 '22 14:10

saurabh yadav


Try:

if($("option[value='parcel']").is(":checked"))
   $('#row_dim').show();

Or even:

$(function() {
    $('#type').change(function(){
        $('#row_dim')[ ($("option[value='parcel']").is(":checked"))? "show" : "hide" ]();  
    });
});

JSFiddle: http://jsfiddle.net/3w5kD/

like image 20
Ivan Chernykh Avatar answered Oct 21 '22 14:10

Ivan Chernykh


change your jquery method to

$(function () { /* DOM ready */
    $("#type").change(function () {
        alert('The option with value ' + $(this).val());
        //hide the element you want to hide here with
        //("id").attr("display","block"); // to show
        //("id").attr("display","none"); // to hide
    });
});
like image 29
Vinay Pratap Singh Bhadauria Avatar answered Oct 21 '22 15:10

Vinay Pratap Singh Bhadauria