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();
});
});
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.
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.
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.
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.
Use following JQuery. Demo
$(function() {
$('#row_dim').hide();
$('#type').change(function(){
if($('#type').val() == 'parcel') {
$('#row_dim').show();
} else {
$('#row_dim').hide();
}
});
});
<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
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/
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
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With