Reputation: 203
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();
});
});
Upvotes: 13
Views: 162507
Reputation: 1
<div class="col-md-12">
<div class="col-md-4">
<label class="form-control" >Pay Mode</label>
<select id="pay_mode2" class="btn-default form-control ">
<option value="cheque">Cheque</option>
<option value="cash" selected>Cash</option>
</select>
</div>
<div class="col-md-4 cheque">
<label class="form-control" >Cancelled</label>
<select class="btn-default form-control ">
<option value="no">No</option>
<option value="yes">Yes</option>
</select>
</div>
<div class="col-md-4 cheque">
<label class="form-control" >Instrument No</label>
<input type="text" class="form-control" name="">
</div>
</div>
<script>
$('#pay_mode2').change(function(){
var myID = $(this).val();
if(myID == 'cheque')
{
$('.cheque').each(function(){
$(this).show();
});
}else{
$('.cheque').each(function(){
$(this).hide();
});
}
});
</script>
Upvotes: 0
Reputation: 13
I used the following jQuery-based snippet to have a select
-element show a div
-element that has an id
that matches the value
of the option
-element while hiding the div
s that do not match. Not sure that it's the best way, but it is a way.
$('#sectionChooser').change(function(){
var myID = $(this).val();
$('.panel').each(function(){
myID === $(this).attr('id') ? $(this).show() : $(this).hide();
});
});
.panel {display: none;}
#one {display: block;}
<select id="sectionChooser">
<option value="one" selected>Thing One</option>
<option value="two">Thing Two</option>
<option value="three">Thing Three</option>
</select>
<div class="panel" id="one">
<p>Thing One</p>
</div>
<div class="panel" id="two">
<p>Thing Two</p>
</div>
<div class="panel" id="three">
<p>Thing Three</p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Upvotes: 0
Reputation: 575
<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
Upvotes: 1
Reputation: 2787
Nothing new but caching your jQuery collections will have a small perf boost
$(function() {
var $typeSelector = $('#type');
var $toggleArea = $('#row_dim');
$typeSelector.change(function(){
if ($typeSelector.val() === 'parcel') {
$toggleArea.show();
}
else {
$toggleArea.hide();
}
});
});
And in vanilla JS for super speed:
(function() {
var typeSelector = document.getElementById('type');
var toggleArea = document.getElementById('row_dim');
typeSelector.onchange = function() {
toggleArea.style.display = typeSelector.value === 'parcel'
? 'block'
: 'none';
};
});
Upvotes: 0
Reputation: 2303
Use following JQuery. Demo
$(function() {
$('#row_dim').hide();
$('#type').change(function(){
if($('#type').val() == 'parcel') {
$('#row_dim').show();
} else {
$('#row_dim').hide();
}
});
});
Upvotes: 48
Reputation: 28837
Try this:
$(function () {
$('#row_dim').hide(); // this line you can avoid by adding #row_dim{display:none;} in your CSS
$('#type').change(function () {
$('#row_dim').hide();
if (this.options[this.selectedIndex].value == 'parcel') {
$('#row_dim').show();
}
});
});
Upvotes: 0
Reputation: 9947
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
});
});
Upvotes: 1
Reputation: 1372
Try this:
$(function() {
$("#type").change(function() {
if ($(this).val() === 'parcel') $("#row_dim").show();
else $("#row_dim").hide();
}
}
Upvotes: 0
Reputation: 1294
Try the below JS
$(function() {
$('#type').change(function(){
$('#row_dim').hide();
if ($(this).val() == 'parcel')
{
$('#row_dim').show();
}
});
});
Upvotes: 0
Reputation: 42166
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/
Upvotes: 3