javascript to limit form options on multiple level children inputs - javascript

I have the following form, in summary:
kingdom --> phylum --> class --> order --> family --> genus...
If the kingdom = Animalia then the phylum options should be a certain list.
Following on, if the phylum then = Chordata the next drop down input option should be a certain list.
To continue, if the class = Mammalia the next drop down input option should be a certain list... etc.
So a hierarchical form with many dependant levels.
I've looked and tried a few examples, but I can't find a way to do this. Any ideas/suggestions?
Here's the bootstrap html:
<div class='col-12 left'>
<div class='form-group'>
<label for='uname'>kingdom</label>
<!-- <input type='text' class='form-control' name='kingdom' value ='Animalia' placeholder='Animalia' > -->
<select class="form-control" name='kingdom'>
<option>Animalia</option>
<option>Plantae</option>
<option>Fungi</option>
<option>Protista</option>
<option>Monera</option>
</select>
<div class='valid-feedback'>Valid.</div>
<div class='invalid-feedback'>Please fill out this field.</div>
</div>
</div>
<div class='col-12 left'>
<div class='form-group'>
<label for='uname'>phylum/division</label>
<!-- <input type='text' class='form-control' name='phylum' value ='Chordata' placeholder='Chordata' > -->
<select class="form-control" name='phylum'>
<option>Chordata</option>
<option>Annelid</option>
<option>Arthropod</option>
<option>Bryozoa</option>
<option>Cnidaria</option>
<option>Echinoderm</option>
<option>Mollusc</option>
<option>Nematode</option>
<option>Platyhelminthes</option>
<option>Rotifer</option>
<option>Sponge</option>
</select>
<div class='valid-feedback'>Valid.</div>
<div class='invalid-feedback'>Please fill out this field.</div>
</div>
</div>
<div class='col-12 left'>
<div class='form-group'>
<label for='uname'>class</label>
<!-- <input type='text' class='form-control' name='class' value ='Mammalia' placeholder='Mammalia' > -->
<select class="form-control" name='class'>
<option>Mammalia</option>
<option>Aves</option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
</select>
<div class='valid-feedback'>Valid.</div>
<div class='invalid-feedback'>Please fill out this field.</div>
</div>
</div>
<div class='col-12 left'>
<div class='form-group'>
<label for='uname'>order</label>
<!-- <input type='text' class='form-control' name='order' value ='Artiodactyla' placeholder='Artiodactyla' > -->
<select class="form-control" name='order'>
<option>Artiodactyla</option>
<option>Aegotheliformes</option>
<option>Perissodactyla</option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
</select>
<div class='valid-feedback'>Valid.</div>
<div class='invalid-feedback'>Please fill out this field.</div>
</div>
</div>
<div class='col-12 left'>
<div class='form-group'>
<label for='uname'>family</label>
<!-- <input type='text' class='form-control' name='family' value ='Bovidae' placeholder=' Bovidae' > -->
<select class="form-control" name='family'>
<option>Bovidae</option>
<option>Aegothelidae</option>
<option>Equidae</option>
<option>Delphinidae</option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
</select>
<div class='valid-feedback'>Valid.</div>
<div class='invalid-feedback'>Please fill out this field.</div>
</div>
</div>
<div class='col-12 left'>
<div class='form-group'>
<label for='uname'>genus</label>
<input type='text' class='form-control' name='genus' value ='Capra' placeholder='Capra' >
<select class="form-control" name='genus'>
<option>Capra</option>
<option>Ovis</option>
<option>Aegotheles</option>
<option>Oreamnos</option>
<option>Equus</option>
<option>Orcinus</option>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
</select>
<div class='valid-feedback'>Valid.</div>
<div class='invalid-feedback'>Please fill out this field.</div>
</div>
</div>
<div class='col-12 left'>
<div class='form-group'>
<label for='uname'>subgenus</label>
<input type='text' class='form-control' name='subgenus' value ='Capra (aegagrus) hircus' placeholder='Capra (aegagrus) hircus' >
<div class='valid-feedback'>Valid.</div>
<div class='invalid-feedback'>Please fill out this field.</div>
</div>
</div>
<div class='col-12 left'>
<div class='form-group'>
<label for='uname'>elementName</label>
<!-- <input type='text' class='form-control' name='elementName' value ='a' placeholder='' > -->
<select class="form-control" name='elementName'>
<option>Cranium</option>
<option>Mandible</option>
<option>Teeth</option>
<option>Rib</option>
<option>Humerus</option>
<option>Ulna</option>
<option>Radius</option>
<option>Carpal</option>
<option>Metacarpal</option>
<option>Femur</option>
<option>Patela</option>
<option>Fibula</option>
<option>Tarsal</option>
<option>Metatarsal</option>
<option>Scapula</option>
<option>Vertebrae</option>
<option>Middle Phalanx</option>
<option>Phalanx</option>
<option>Tibia</option>
<option>Astragalus</option>
<option>Calcaneus</option>
<option>Proximal Phalanx</option>
</select>
<div class='valid-feedback'>Valid.</div>
<div class='invalid-feedback'>Please fill out this field.</div>
</div>
</div>

maybe little bit lazy, but it works
if you thought similar
<div class="form-group" >
<select class="form-control" id="sel0">
<option value="0">Choose</option>
<option value="1">Audi</option>
<option value="2">BMW</option>
</select>
<br>
<select class="form-control dispnone" id="sel1_1">
<option value="0">Choose</option>
<option value="1">A4</option>
<option value="2">RS8</option>
</select>
<select class="form-control dispnone" id="sel1_2">
<option value="0">Choose</option>
<option value="1">E36</option>
<option value="2">X5</option>
</select>
<select class="form-control dispnone" id="sel2_1">
<option value="0">Choose</option>
<option value="1">120LE</option>
<option value="2">140LE</option>
</select>
<select class="form-control dispnone" id="sel2_2">
<option value="0">Choose</option>
<option value="1">cyl8</option>
<option value="2">cyl16</option>
</select>
<script type="text/javascript">
$(document).ready(function() {
var select_nums = 3;
$("#sel0").change(function() {
var ch = $("#sel0").val();
if(ch==0){
for (var i = 1; i < select_nums; i++) {
$('#sel1_'+i).css({'display':'none'});
}
$('#sel1_'+ch).css({'display':'block'});
}else if(ch==1){
for (var i = 1; i < select_nums; i++) {
$('#sel1_'+i).css({'display':'none'});
}
$('#sel1_'+ch).css({'display':'block'});
}else if(ch==2){
for (var i = 1; i < select_nums; i++) {
$('#sel1_'+i).css({'display':'none'});
}
$('#sel1_'+ch).css({'display':'block'});
}
});
$("#sel1_1").change(function() {
var ch = $("#sel1_1").val();
if(ch==0){
for (var i = 1; i < select_nums; i++) {
$('#sel2_'+i).css({'display':'none'});
}
$('#sel2_'+ch).css({'display':'block'});
}else if(ch==1){
for (var i = 1; i < select_nums; i++) {
$('#sel2_'+i).css({'display':'none'});
}
$('#sel2_'+ch).css({'display':'block'});
}else if(ch==2){
for (var i = 1; i < select_nums; i++) {
$('#sel2_'+i).css({'display':'none'});
}
$('#sel2_'+ch).css({'display':'block'});
}
});
});
</script>

I extracted a portion of your html and got it working using an event listener to hide the unwanted options. I hope it helps:
I only considered the first two categories to show the example 'Animalia' and 'Plantae' and considered two subsets of options for each. The change event on the select tag triggers the code to hide and show as required.
function hideSubset(selectedValue){
let secondChoice = document.querySelector('#secondChoice');
let firstChoice = document.querySelector('#firstChoice');
if (selectedValue === 'Animalia'){
secondChoice.style.display = 'none';
firstChoice.style.display = 'block';
} else {
firstChoice.style.display = 'none';
secondChoice.style.display = 'block';
}
}
let mainChoice = document.querySelector('#mainChoice');
mainChoice.addEventListener('change', function(){
hideSubset(mainChoice.value);
}, false);
<div class='col-12 left'>
<div class='form-group'>
<label for='uname'>kingdom</label>
<!-- <input type='text' class='form-control' name='kingdom' value ='Animalia' placeholder='Animalia' > -->
<select class="form-control" name='kingdom' id="mainChoice">
<option>Choose one</option>
<option>Animalia</option>
<option>Plantae</option>
</select>
<div class='valid-feedback'>Valid.</div>
<div class='invalid-feedback'>Please fill out this field.</div>
</div>
</div>
<div class='col-12 left' id="firstChoice">
<div class='form-group'>
<label for='uname'>Options for Animalia</label>
<!-- <input type='text' class='form-control' name='phylum' value ='Chordata' placeholder='Chordata' > -->
<select class="form-control" name='phylum'>
<option>Chordata</option>
<option>Annelid</option>
<option>Arthropod</option>
<option>Bryozoa</option>
<option>Cnidaria</option>
<option>Echinoderm</option>
</select>
<div class='valid-feedback'>Valid.</div>
<div class='invalid-feedback'>Please fill out this field.</div>
</div>
</div>
<div class='col-12 left' id="secondChoice">
<div class='form-group'>
<label for='uname'>Options for Plantae</label>
<!-- <input type='text' class='form-control' name='phylum' value ='Chordata' placeholder='Chordata' > -->
<select class="form-control" name='phylum'>
<option>Mollusc</option>
<option>Nematode</option>
<option>Platyhelminthes</option>
<option>Rotifer</option>
<option>Sponge</option>
</select>
<div class='valid-feedback'>Valid.</div>
<div class='invalid-feedback'>Please fill out this field.</div>
</div>
</div>
It shows like this on my side. I use bootstrap 4
Looks like this when loading
When choosing Animalia
When choosing Plantae

Related

Show/hide div and clear filed value when change

I have some div that will show depending on the select value. Suppose there are two input fields name 'Apartment and Bechelor' If I select 'Apartment' some div and select field will show related to the apartment. The problem I am facing is, I can't clear the field value when selecting another. Like, after selecting 'Apartment', I will select Bachelor. Now it should be clear the value of the field which was under the apartment so that If I again select 'Apartment', I will get default values or clean fields.
<div id="residential" >
<input type="radio" value="apartment" id="type_apartment" name="type" >
<label for="type_apartment" class="radio-inline"> Apartment/Flat </label>
<input type="radio" value="bachelor" id="type_bachelor" name="type">
<label for="type_bachelor" class="radio-inline"> Bechelor </label>
</div>
<!-- show this when apartment select -->
<div class="form-group " id="tenant-flat">
<select id="tenant-type" class="form-control" name="tenant">
<option value="">Anyone</option>
<option value="family">Family</option>
<option value="bechelor" >Bechelor</option>
</select>
</div>
<div class="form-group " id="flat-type">
<div class="col-sm-12">
<select id="" class="form-control" name="flat-type">
<option value="">Select Flat Type</option>
<option value="sublet" >Sublet</option>
<option value="full-flat" >Full Flat</option>
</select>
</div>
</div>
<!-- show this when bechelor select -->
<div class="form-group " id="r-type">
<div class="col-sm-12">
<select id="room" class="form-control" name="room_type">
<option value="">Select Room Type</option>
<option value="seat">Seat</option>
<option value="room" >Room</option>
</select>
</div>
</div>
<div class="form-group " id="tenant">
<div class="col-sm-12">
<select id="" class="form-control" name="tenant">
<option value="">Select Member</option>
<option value="family" >Family</option>
<option value="student" >Student</option>
<option value="job-holder" >Job Holder</option>
</select>
</div>
</div>
<script>
$(document).ready(function(){
$('#r-type').hide();
$('#tenant').hide();
$('#tenant-flat').hide();
$('#flat-type').hide();
$('.pretty input[type="radio"]').click(function(){
if($(this).attr('value') == 'apartment'){
$('#tenant-flat').show();
}
else{
$('#flat-type').hide();
}
});
var showDiv = document.getElementById("tenant-type");
showDiv.onchange = function(){
var hiddenDiv = document.getElementById("flat-type");
hiddenDiv.style.display = (this.value == "family") ? "block":"none";
var genderDiv = document.getElementById("gender");
genderDiv.style.display = (this.value == "bechelor") ? "block":"none";
};
$('.pretty input[type="radio"]').click(function(){
if($(this).attr('value') == 'bachelor' || $(this).attr('value') == 'sublet' || $(this).attr('value') == 'hostel' ){
$('#r-type').show();
$('#tenant').show();
$('#tenant-type').hide();
}
else{
$('#r-type').hide();
$('#tenant').hide();
$('#tenant-type').show();
}
});
});
</script>
to reset the dropdown options you can set the value of the select elements selectIndex property to 0 with this line:
$('.form-control').prop('selectedIndex', 0);
and in your code:
$(document).ready(function(){
$('#r-type').hide();
$('#tenant').hide();
$('#tenant-flat').hide();
$('#flat-type').hide();
$('#residential input[type="radio"]').click(function(){
if($(this).attr('value') == 'apartment'){
$('#tenant-flat').show();
$('#flat-type').show();
$('#r-type').hide();
$('#tenant').hide();
}
else{
$('#r-type').show();
$('#tenant').show();
$('#flat-type').hide();
$('#tenant-flat').hide();
}
$('.form-control').prop('selectedIndex', 0);
});
$('#tenent-type').on('change', function() {
if($('#flat-type').val =='family') {
$('#flat-type').show();
} else {
$('#flat-type').hide();
}
if($('#gender').val =='bechelor') {
$('#gender').show();
} else {
$('#gender').hide();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="residential" >
<input type="radio" value="apartment" id="type_apartment" name="type" >
<label for="type_apartment" class="radio-inline"> Apartment/Flat </label>
<input type="radio" value="bachelor" id="type_bachelor" name="type">
<label for="type_bachelor" class="radio-inline"> Bechelor </label>
</div>
<!-- show this when apartment select -->
<div class="form-group " id="tenant-flat">
<select id="tenant-type" class="form-control" name="tenant">
<option value="">Anyone</option>
<option value="family">Family</option>
<option value="bechelor" >Bechelor</option>
</select>
</div>
<div class="form-group " id="flat-type">
<div class="col-sm-12">
<select id="" class="form-control" name="flat-type">
<option value="">Select Flat Type</option>
<option value="sublet" >Sublet</option>
<option value="full-flat" >Full Flat</option>
</select>
</div>
</div>
<!-- show this when bechelor select -->
<div class="form-group " id="r-type">
<div class="col-sm-12">
<select id="room" class="form-control" name="room_type">
<option value="">Select Room Type</option>
<option value="seat">Seat</option>
<option value="room" >Room</option>
</select>
</div>
</div>
<div class="form-group " id="tenant">
<div class="col-sm-12">
<select id="" class="form-control" name="tenant">
<option value="">Select Member</option>
<option value="family" >Family</option>
<option value="student" >Student</option>
<option value="job-holder" >Job Holder</option>
</select>
</div>
</div>

how to clone div bassed select option

i have select option with count of numbers so i need now when user choose any number clone div tag personal information by this number ex: - select 8 clone 8 time ..
i try do that by use( for loop )but i have problem when run i see clone over this number ex:- choose 8 clone 16 time
this is html code and my try js code
$('#c3').change(function() {
$('.cloneHere').empty();
var count = $('#c3').val();
for (i = 1; count > i; i++) {
var clone = $('.rowClone').clone();
$('.cloneHere').append(clone);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='col-xs-3'>
<label for="">count of person</label>
<select class='form-control select2' name="" id="c3">
<option value="" selected disabled hidden>Choose here</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
</div>
</div>
</div>
</div>
</div>
</div>
<div class='col-md-12'>
<div class='box box-primary'>
<div class='box-header with-border'>
<h3 class='box-title'>Information of Person</h3>
</div>
<div class='form-group'>
<div class='box-body rowClone2'>
<div class=' row'>
<div class=' col-sm-3'><label for="">Person 1</label></div>
</div>
<div class='row rowClone'>
<div class='col-xs-6 col-md-3'>
<input type="text" class="form-control">
</div>
<div class='col-xs-6 col-md-3'>
<input type="text" class="form-control">
</div>
<div class='col-xs-6 col-md-3'>
<input type="tel" class="form-control">
</div>
<div class='col-xs-6 col-md-3'>
<input type="text" class="form-control">
</div>
</div>
<br>
<div class="row cloneHere">
</div>
</div>
The problem you are facing comes from this line: var clone = $('.rowClone').clone();
the first clone is fine because only 1 .rowClone exist, next time multiple .rowClone exist and it appends all of those.
You have 2 solutions, either use var clone = $('.rowClone:first').clone(); or move var clone = $('.rowClone').clone(); before your for loop
Demo of the problem, run it and look at the console
$('#c3').change(function() {
$('.cloneHere').empty();
var count = $('#c3').val();
for (i = 1; count > i; i++) {
console.log("Number of .rowClone that exist = " + $('.rowClone').length)
var clone = $('.rowClone').clone();
$('.cloneHere').append(clone);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='col-xs-3'>
<label for="">count of person</label>
<select class='form-control select2' name="" id="c3">
<option value="" selected disabled hidden>Choose here</option>
<option value="8">8</option>
</select>
</div>
<div class='col-md-12'>
<div class='box box-primary'>
<div class='box-header with-border'>
<h3 class='box-title'>Information of Person</h3>
</div>
<div class='form-group'>
<div class='box-body rowClone2'>
<div class=' row'>
<div class=' col-sm-3'><label for="">Person 1</label></div>
</div>
<div class='row rowClone'>
<div class='col-xs-6 col-md-3'>
<input type="text" class="form-control">
</div>
<div class='col-xs-6 col-md-3'>
<input type="text" class="form-control">
</div>
<div class='col-xs-6 col-md-3'>
<input type="tel" class="form-control">
</div>
<div class='col-xs-6 col-md-3'>
<input type="text" class="form-control">
</div>
</div>
<br>
<div class="row cloneHere">
</div>
</div>
</div>
</div>
</div>

Last-child not working inside iteration of div

In browser, the HTML is generated dynamically and is rendered as
<div id="dynamic-relationship-details">
<div id="count-status0" class="relationship-container form-group">
<div class="col-sm-1"></div>
<div class="col-sm-2">
<select id="relationship-type0" class="form-control"><option value="">Select Relationship</option><option value="Father">Father</option><option value="Mother">Mother</option><option value="Brother">Brother</option><option value="Sister">Sister</option><option value="Spouse">Spouse</option><option value="Guardian">Guardian</option></select>
</div>
<div class="col-sm-3">
<input type="text" name="relationship-type-name0" id="relationship-type-name0" class="form-control" placeholder="Name"></div><div class="col-sm-2"><input type="text" name="relationship-type-contact0" id="relationship-type-contact0" class="form-control" placeholder="Contact Number">
</div>
<button value="count-status0" class="remove-relationship-field btn btn-danger"><i class="fa fa-trash"></i></button>
</div><div id="count-status1" class="relationship-container form-group">
<div class="col-sm-1"></div>
<div class="col-sm-2">
<select id="relationship-type1" class="form-control"><option value="">Select Relationship</option><option value="Father">Father</option><option value="Mother">Mother</option><option value="Brother">Brother</option><option value="Sister">Sister</option><option value="Spouse">Spouse</option><option value="Guardian">Guardian</option></select>
</div>
<div class="col-sm-3">
<input type="text" name="relationship-type-name1" id="relationship-type-name1" class="form-control" placeholder="Name"></div><div class="col-sm-2"><input type="text" name="relationship-type-contact1" id="relationship-type-contact1" class="form-control" placeholder="Contact Number">
</div>
<button value="count-status1" class="remove-relationship-field btn btn-danger"><i class="fa fa-trash"></i></button>
</div>
</div>
The code is
// Relationship details Array
$(".relationship-container").each(function(i, obj) {
var $this = $(this);
$this.find("select").each(function() {
var relationshipTypeValue = $(this).val();
var relationshipName =$this.find("input[type=text]:first-child").val();
var relationshipContactNumber =$this.find("input[type=text]:last-child").val();
var innerRelationshipArray = {};
innerRelationshipArray = {
relationshipTypeValue: relationshipTypeValue,
relationshipName: relationshipName,
relationshipContactNumber: relationshipContactNumber
};
relationship_details_array.push(innerRelationshipArray);
});
});
I am trying to fetch values of contact numbers i.e with id's relationship-type-contact(n) values in the line "
var relationshipContactNumber =$this.find("input[type=text]:last-child").val();"
This is fetching values of first-child textboxes in the variable relationshipContactNumber in the loop.
Please help !!!
This should work, use the .first() and .last() selectors.
Another way, if you have access to alter the HTML, is to add class names for the input fields and select them by using that instead.
function getData() {
var relationship_details_array = [];
// Relationship details Array
$(".relationship-container").each(function(i, obj) {
var $this = $(this);
$this.find("select").each(function() {
var relationshipTypeValue = $(this).val();
var relationshipName = $this.find("input[type=text]").first().val();
var relationshipContactNumber = $this.find("input[type=text]").last().val();
var innerRelationshipArray = {};
innerRelationshipArray = {
relationshipTypeValue: relationshipTypeValue,
relationshipName: relationshipName,
relationshipContactNumber: relationshipContactNumber
};
relationship_details_array.push(innerRelationshipArray);
});
});
console.log(relationship_details_array);
}
$("#getData").on("click", getData);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="getData">Show data in console log</button>
<div id="dynamic-relationship-details">
<div id="count-status0" class="relationship-container form-group">
<div class="col-sm-1"></div>
<div class="col-sm-2">
<select id="relationship-type0" class="form-control">
<option value="">Select Relationship</option>
<option value="Father">Father</option>
<option value="Mother" selected>Mother</option>
<option value="Brother">Brother</option>
<option value="Sister">Sister</option>
<option value="Spouse">Spouse</option>
<option value="Guardian">Guardian</option>
</select>
</div>
<div class="col-sm-3">
<input type="text" name="relationship-type-name0" id="relationship-type-name0" class="form-control" value="Mommy" placeholder="Name">
</div>
<div class="col-sm-2">
<input type="text" name="relationship-type-contact0" id="relationship-type-contact0" class="form-control" value="1234" placeholder="Contact Number">
</div>
</div>
<div id="count-status1" class="relationship-container form-group">
<div class="col-sm-1"></div>
<div class="col-sm-2">
<select id="relationship-type1" class="form-control">
<option value="">Select Relationship</option>
<option value="Father" selected>Father</option>
<option value="Mother">Mother</option>
<option value="Brother">Brother</option>
<option value="Sister">Sister</option>
<option value="Spouse">Spouse</option>
<option value="Guardian">Guardian</option>
</select>
</div>
<div class="col-sm-3">
<input type="text" name="relationship-type-name1" id="relationship-type-name1" class="form-control" value="Daddy" placeholder="Name">
</div>
<div class="col-sm-2">
<input type="text" name="relationship-type-contact1" id="relationship-type-contact1" class="form-control" value="5678" placeholder="Contact Number">
</div>
</div>
</div>

javascript hide/show function is not working

This is the basic html page
<div class="col-lg-10">
<div class="form-group">
<select class="form-control" id="">
<option value="" hidden>Select Venue Type</option>
<option onclick="myFunction()">Theme Restaurant</option>
<option onclick="myFunction()">Blah restaurant</option>
<option onclick="myFunction()">Flana Restaurant</option>
<option onclick="myFunction()">Woops Restaurant</option>
</select>
</div>
<div class="form-group">
<div class="venue-type" id="restaurant-form">
<div class="row">
<div class="col-md-5" id="bsc-edit">
<div class="form-group">
<label for="city">Venue Name<sup class = "venue-imp">*</sup>
</label>
<input type="text" class="form-control" id="venue-name" placeholder="Jhanqar Banquet Hall" name="name">
</div>
</div>
<div class="col-md-1"></div>
<div class="col-md-5">
<div class="form-group">
<label for="city">Venue Price<sup class = "venue-imp">*</sup>
</label>
<input type="text" class="form-control" id="venue-price" placeholder="Jhanqar Banquet Hall" name="price">
</div>
</div>
<div class="col-md-1"></div>
</div>
</div>
</div>
</div>
i want my dropdown list to show a form when clicked and i have tried using javascript function here, but it is not working the form is showing as it is and nothing is working with onclick. Help me to do it as the form is not being shown when clicked but it is shown directly
this is the javascript code;
<script>
function myFunction() {
var x = document.getElementById("restaurant-form");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
Yes as per #gurvder372 you can't use onclick for option.You can also use an inline event assignment for this Like:
<select class="form-control" id="" onchange="javascript:return myFunction(event);">
<option value="" hidden>Select Venue Type</option>
<option>Theme Restaurant</option>
<option>Blah restaurant</option>
<option>Flana Restaurant</option>
<option>Woops Restaurant</option>
</select>
<script>
function myFunction(event)
{
var index_opt = event.target.selectedIndex;
if ( index_opt > 0 )
{
var x = document.getElementById("restaurant-form");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
}
</script>
onclick is not supported on option tag, You need to add event listener to the select instead
document.querySelector( "select[id]" ).addEventListener( "change", myFunction );
In the myFunction method you can show/hide form based on which option is selected.
function myFunction(event)
{
var selection = event.target.selectedIndex;
var x = document.getElementById("restaurant-form");
x.style.display = selection >= 1 ? "block" : "none";
}
Demo
function myFunction(event)
{
var selection = event.target.selectedIndex;
var x = document.getElementById("restaurant-form");
x.style.display = selection >= 1 ? "block" : "none";
}
document.querySelector( "select[id]" ).addEventListener( "change", myFunction );
<div class="col-lg-10">
<div class="form-group">
<select class="form-control" id="" >
<option value="">Select Venue Type</option>
<option>Theme Restaurant</option>
<option>Blah restaurant</option>
<option>Flana Restaurant</option>
<option>Woops Restaurant</option>
</select>
</div>
<div class="form-group">
<div class="venue-type" id="restaurant-form">
<div class="row">
<div class="col-md-5" id="bsc-edit">
<div class="form-group">
<label for="city">Venue Name<sup class = "venue-imp">*</sup>
</label>
<input type="text" class="form-control" id="venue-name" placeholder="Jhanqar Banquet Hall" name="name">
</div>
</div>
<div class="col-md-1"></div>
<div class="col-md-5">
<div class="form-group">
<label for="city">Venue Price<sup class = "venue-imp">*</sup>
</label>
<input type="text" class="form-control" id="venue-price" placeholder="Jhanqar Banquet Hall" name="price">
</div>
</div>
<div class="col-md-1"></div>
</div>
</div>
</div>
</div>

How to Show/Hide Div on SelectedIndex in Bootstrap 3?

I have this code:
<div class="form-group">
<label for="slDisThrough" class="col-sm-1 control-label">Distributed Through</label>
<div class="col-sm-11">
<select name="slDisThrough" id="slDisThrough" class="form-control">
<option value="0">-Select-</option>
<option value="1">Agent</option>
<option value="2">Retail</option>
</select>
</div>
</div>
<div id="slAgentID" class="hide">
<div class="form-group">
<label for="slAgent" class="col-sm-1 control-label">Agent</label>
<div class="col-sm-11">
<select name="slAgent" class="form-control">
<option value="0">-Select-</option>
<option value="1">Show</option>
<option value="2">Hide</option>
</select>
</div>
</div>
</div>
<div id="slRetailID" class="hide">
<div class="form-group">
<label for="slRetail" class="col-sm-1 control-label">Retail</label>
<div class="col-sm-11">
<select name="slRetail" class="form-control">
<option value="0">-Select-</option>
<option value="1">Hide</option>
<option value="2">Show</option>
</select>
</div>
</div>
</div>
Check this Fiddle
if(document.getElementById("slDisThrough").selectedIndex == 1) {
document.getElementById("slAgentID").style.display = ""; }
else { document.getElementById("slAgentID").style.display = "none"; }
if(document.getElementById("slDisThrough").selectedIndex == 2) {
document.getElementById("slRetail").style.display = ""; }
else { document.getElementById("slRetail").style.display = "none"; }
I need to Show/Hide the DIVs on Selected Value, I was previously using it with JavaScript, but stuck with Bootstrap.
Since you load jQuery, better use it like this:
$('#slDisThrough').on('change', function(){
if ($(this).val() == 1 ) {
$('#slAgentID').removeClass('hide');
$('#slRetailID').addClass('hide');
}
if ($(this).val() == 2 ) {
$('#slAgentID').addClass('hide');
$('#slRetailID').removeClass('hide');
}
})
Jsfiddle here

Categories