Show a new select for option selected dynamically jquery - javascript

<div class="form-group">
<label for="options">foo</label>
<select class="form-control" name="options" multiple>
<option value="">-- Choice --</option>
<option value="opt1">test1</option>
<option value="opt2">test2</option>
<option value="opt3">test3</option>
</select>
</div>
<script type="text/javascript">
$("#scope_new_system").change(function() {
var allSelected = new Array();
$("#scope_new_system option:selected").each(function() {
allSelected.push(this.value);
});
});
</script>
I'm trying to dynamically generate a select with different options based on options selected on the multiple select named "options" above. For example if I click on option test 1 then a select element with options shows to select an option. If I select test 2 then another select shows because I now have 2 options selected. Finally if I unclicked test 1 select option goes away and if i do the same with test 2 it also goes away as well and i'm left with nothing selected. Please help!.

Is this what you need?
$('#selectpicker').multiselect();
$('#selectpicker').on('change',function() {
var val = $(this).val();
$('.hidden_slc').hide();
for (x in val) {
$('#based'+val[x]).show();
}
})
<style>
.hidden_slc {
display: none;
}
</style>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css" rel="stylesheet"/>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>
<select id="selectpicker" multiple>
<option value='1'>tes1</option>
<option value='2'>tes2</option>
<option value='3'>tes3</option>
</select>
<select id="based1" class='hidden_slc'>
<option>sample_from_1</option>
<option>sample</option>
</select>
<select id="based2" class='hidden_slc'>
<option>sample_from_2</option>
<option>sample</option>
</select>
<select id="based3" class='hidden_slc'>
<option>sample_from_3</option>
<option>sample</option>
</select>

You can use this.
var allSelected = [];
$("#scope_new_system").on("change",function() {
var allSelected = new Array();
$("#scope_new_system option:selected").each(function(){
allSelected.push(this.value);
});
console.log(allSelected)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div class="form-group">
<label for="options">foo</label>
<select id="scope_new_system" class="form-control" name="options" multiple>
<option value="">-- Choice --</option>
<option value="opt1">test1</option>
<option value="opt2">test2</option>
<option value="opt3">test3</option>
</select>
</div>

As I pointed So the relation between the two selects just to show/hide it?? or select 2 values depending on select 1 values?? I didn't got that point
But sure Your 1st step is: To check for option:selected length by using $("option:selected" , this).length; or $(this).find("option:selected").length;
$("#scope_new_system").change(function() {
var allSelected = [],
num_of_selected_option = $("option:selected" , this).length;
if(num_of_selected_option == 0){
// if nothing selected
alert('Nothing Selected');
}else if(num_of_selected_option == 1){
// if just one selected
allSelected = [this.value];
alert('Just one value selected : '+allSelected);
}else if(num_of_selected_option > 1){
// if more than one selected
$("option:selected" , this).each(function(){
allSelected.push(this.value);
});
alert('Selected Values : '+allSelected);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<label for="options">foo</label>
<select id="scope_new_system" class="form-control" name="options" multiple>
<option value="">-- Choice --</option>
<option value="opt1">test1</option>
<option value="opt2">test2</option>
<option value="opt3">test3</option>
</select>
</div>

Related

how to populate dropdown list based on previous dropdown selected value

having some issues while updating data, I have two dropdown list and using bootstrap select picker I need to update second dropdown list based on first one but failed.pls kindly help
Posting the fiddle link
fiddlelink
$('.selectpicker').on('changed.bs.select', function (e) {
if($(this).attr('name')=="name2"){
$(".selectpicker[name='name1']").val();
}
else{
$(".selectpicker[name='name2']").val();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="id1" name="name1" class="selectpicker" data-style="form-
control" data-live-search="true" title="Select Fruits"
multiple="multiple">
<option value="Fruit">Fruits</option>
<option value="Animal">Animal</option>
</select>
<select id="id2" name="name2" class="selectpicker" data-style="form-control" data-live-search="true" title="Select Product"
multiple="multiple">
<option value="Fruit">Mangoes</option>
<option value="Fruit">Apple</option>
<option value="Animal">Dog</option>
<option value="Animal">Cat </option>
</select>
You can use change event on your select-box . Inside this get value of selected option using $(this).val() this will return you array so use for-loop to iterate through value and show options where value matches in second dropdown . Lastly refresh your selectpicker to update changes .
Demo Code :
$('#id1').on('change', function(e) {
var values = $(this).val()
$("#id2 option").hide() //hide all options
$('#id2').selectpicker('deselectAll') //if want to remove all selcted optn
if (values.length > 0) {
for (var i = 0; i < values.length; i++) {
//show where value is same..
$("#id2 option[value=" + values[i] + "]").show()
}
} else {
$("#id2 option").show() //show all options
}
$('#id2').selectpicker('refresh'); //refresh selctpicker
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-select#1.13.14/dist/css/bootstrap-select.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap-select#1.13.14/dist/js/bootstrap-select.min.js"></script>
<select id="id1" name="name1" class="selectpicker" data-style="form-control" data-live-search="true" title="Select Fruits" multiple="multiple">
<option value="Fruit">Fruits</option>
<option value="Animal">Animal</option>
</select>
<select id="id2" name="name2" class="selectpicker" data-style="form-control" data-live-search="true" title="Select Product" multiple="multiple">
<option value="Fruit">Mangoes</option>
<option value="Fruit">Apple</option>
<option value="Animal">Dog</option>
<option value="Animal">Cat </option>
</select>

How to reset default value in an select based on previous select tag?

I have two select options :
<select id="Living things">
<option>Choose Any</option>
<option>Animals</option>
<option>Plants</option>
</select>
<select id="Compare">
<option>Any</option>
<option>less</option>
<option>greater</option>
</select>
Consider the first options from both the drop downs are default. Now imagine I selected 'Animals' from first and 'less' from second. Now if I change the first to 'Plants' then the second list should be reset.i.e,
<option>Any</option>
<option>less</option>
<option>greater</option>
Please help in this.Thank You.
<select id="livingThings" onchange="reset();">
<option>Choose Any</option>
<option>Animals</option>
<option>Plants</option>
</select>
<select id="compare">
<option>Any</option>
<option>less</option>
<option>greater</option>
</select>
<script src="test.js"></script>
livingThings = document.getElementById('livingThings');
compare =document.getElementById('compare');
function reset(){
if (livingThings.selectedIndex != 0){
compare.selectedIndex = 0;
}
}
You can add an event listener for the first select element whenver it changes then reset the value of the second select element to its default, note that IDs can not have white spaces, I have added a dash to it
document.querySelector("#Living-things").onchange = function() {
let select = document.querySelector("#Compare");
select.value = select.children[0].value;
}
<select id="Living-things">
<option>Choose Any</option>
<option>Animals</option>
<option>Plants</option>
</select>
<select id="Compare">
<option>Any</option>
<option>less</option>
<option>greater</option>
</select>

Jquery Select2.js Selected dropdown order

Have checked the below link i am also facing the same issue but i am using select2.js please let me know how can i get the multi value in the same order which i have selected from multiselected dropdown.
jquery multiselect selected data order
There is a Select2 issue, in multi-select: selections do not appear in the order in which they were selected.
Below is a snippet using the workaround proposed in that issue by user xelax90:
$(document).ready(function() {
$("#select").select2({
width: "100%",
});
$("#select").on("select2:select", function (evt) {
var elm = evt.params.data.element;
$elm = $(elm);
$t = $(this);
$t.append($elm);
$t.trigger('change.select2');
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<div class="container">
<select multiple="multiple" id="select">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
</div>

MaterializeCss : How to enable different checkboxes with JQuery in multiple select?

I am having trouble Enabling Check boxes in Select box with multiple attribute. the values are in an array.
var values = ["op1","op2","op3","op4"];
I want to enable all check boxes which are in array. i cant figure out how to pass values to select. the box is working fine when i give only one value
$("#select_box").val(values[0]);
here is the fiddle
https://jsfiddle.net/dmtv02j8/3/
Aparantly you have to call .material_select2('refresh'). This is how you can achieve what you want:
$(document).ready(function() {
var values = ['op1', 'op2'];
var selectEl = $('select');
selectEl.material_select();
selectEl.val(values);
selectEl.material_select('refresh');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/js/materialize.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/css/materialize.min.css" rel="stylesheet"/>
<div class="input-field col s12">
<select multiple>
<option value="" disabled selected>Choose your option</option>
<option value="op1">Option 1</option>
<option value="op2">Option 2</option>
<option value="op3">Option 3</option>
</select>
<label>Materialize Multiple Select</label>
</div>
Just pass data instead data[0] and it works
$('#list').material_select(); // initial initialization
// data from database via ajax
var data = ["op1","op2"];
$('#list').val(data);
$('#list').material_select(); // re initalization on data change
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/js/materialize.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/css/materialize.min.css" rel="stylesheet"/>
<div class="input-field col s12">
<select multiple name="list[]" id="list">
<option value="" disabled selected>Choose your option</option>
<option value="op1">op1</option>
<option value="op2">op2</option>
<option value="op3">op3</option>
<option value="op4">op4</option>
<option value="op5">op5</option>
</select>
<label style="color:#009688">List Of Category</label>
</div>

I need to trigger change function whenever a text in dropdown list is selected

My jquery code is having two dropdown boxes,one containing country and the other one holds currency.so here i want that whenever a country is selected in the box it will trigger a change function that will perform some comparison using if-else and will select the currency in the other dropdown and leaving it as 'disabled' to true.Below is my code for the same:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function () {
$(".select").change(function () {
debugger;
var c = $('#country :selected').text();
if (c == 'india') {
$('select[name="currency"]').find('option[value="1"]').attr("selected", true);
document.getElementById('disable').disabled = true;
}
else
{
$('select[name="currency"]').find('option[value="2"]').attr("selected", true);
document.getElementById('disable').disabled = true;
}
});
});
//$(function () {
// var select = $('select.select');
// select.change(function () {
// select.not(this).val(this.value);
// document.getElementById("disable").disabled = true;
// });
//});
</script>
</head>
<body>
<form>
<select name="country" id="country" class="select">
<option value="">Select</option>
<option value="1">india</option>
<option value="2">America</option>
</select>
<select name="currency" id="disable" class="select">
<option value="">Select</option>
<option value="1">INR</option>
<option value="2">DOLLAR</option>
</select>
<br><br>
</form>
</body>
</html>
but now the problem is when i select india it shows IR which is ok and then when i select America,it shows Doller and finally the problem comes now,when i again change the country back to india the change function is not called at all and dollar gets remain selected and disabled.Fix this it would be a great help!
Use .prop instead of .attr
With .attr, both the options have selected attribute set hence browser fail to make out which option to be set as selected.
From docs, To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method. (.attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior.)
$(document).ready(function() {
$(".select").change(function() {
var c = $('#country :selected').text();
if (c == 'india') {
$('select[name="currency"]').find('option[value="1"]').prop("selected", true);
} else {
$('select[name="currency"]').find('option[value="2"]').prop("selected", true);
}
document.getElementById('disable').disabled = true;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form>
<select name="country" id="country" class="select">
<option value="">Select</option>
<option value="1">india</option>
<option value="2">America</option>
</select>
<select name="currency" id="disable" class="select">
<option value="">Select</option>
<option value="1">INR</option>
<option value="2">DOLLAR</option>
</select>
<br>
<br>
</form>
Since the values on both select are the same. You can easily set the value of the second select based on the first.
$(function() {
$('.select').on('change',function() {
var val = $(this).find(':selected').val();
$('[name=currency]').val( val ).attr('disabled',true);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<select name="country" id="country" class="select">
<option value="">Select</option>
<option value="1">india</option>
<option value="2">America</option>
</select>
<select name="currency">
<option value=""> </option>
<option value="1">INR</option>
<option value="2">DOLLAR</option>
</select>
</form>
To complete the above answer you can use the "$" jquery selector instead of document.getElementById
Plus note you can use .prop('disabled', false);
too, like this :
$(document).ready(function() {
$(".select").change(function() {
var c = $('#country :selected').text();
if (c == 'india') {
$('select[name="currency"]').find('option[value="1"]').prop("selected", true);
} else {
$('select[name="currency"]').find('option[value="2"]').prop("selected", true);
}
$(#'disable').prop('disabled', true);
});
});

Categories