Multiple dropdown menu filter in html - html

Assume I have two dropdown Menu, is it possible to filter menu b by using HTML code only?
If not, which method can be apply for this case with simply code.
menu a with A B
menu b with A1 A2 B1 B2

You can do this with the help of Javascript.
Suppose you have one dropdown with some values as "Colours, Shapes, Names" with html as:
Dropdown1:
<select id="ddl">
<option value=""></option>
<option value="Colours">Colours</option>
<option value="Shapes">Shapes</option>
<option value="Names">Names</option>
</select>
And you want to filter another one based on first dropdown selected value
Dropdown2:
<select id="ddl2">
</select>
Then Apply below Javascript code:
Filter out:
function configureDropDownLists(ddl1,ddl2) {
var colours = new Array('Black', 'White', 'Blue');
var shapes = new Array('Square', 'Circle', 'Triangle');
var names = new Array('John', 'David', 'Sarah');
switch (ddl1.value) {
case 'Colours':
ddl2.options.length = 0;
for (i = 0; i < colours.length; i++) {
createOption(ddl2, colours[i], colours[i]);
}
break;
case 'Shapes':
ddl2.options.length = 0;
for (i = 0; i < shapes.length; i++) {
createOption(ddl2, shapes[i], shapes[i]);
}
break;
case 'Names':
ddl2.options.length = 0;
for (i = 0; i < names.length; i++) {
createOption(ddl2, names[i], names[i]);
}
break;
default:
ddl2.options.length = 0;
}
}
And create option runtime for second dropdown:
function createOption(selBox, text, value) {
var opt = document.createElement('option');
opt.value = value;
opt.text = text;
selBox.options.add(opt);
}
Apply:
onchange="configureDropDownLists(this,document.getElementById('ddl2'))" in first dropdown, as:
<select id="ddl" onchange="configureDropDownLists(this,document.getElementById('ddl2'))">
<option value=""></option>
<option value="Colours">Colours</option>
<option value="Shapes">Shapes</option>
<option value="Names">Names</option>
</select>
So dropdown 2 gets filter based on first selected value.
Please refer to fiddle for live demo
Hope this will help you :)

Related

Matching two ID attributes from cloned dependent dropdown list

In this code I have 2 dependent dropdown lists and a button to duplicate/clone the form. The color selection changes based on what is selected in item. When I duplicate the dropdown list the function didn't work. I tried changing the id of the duplicated dropdown list but still can't manage to match the id of 2 dropdown list. Is there any solution? Thanks.
var count = 1;
var duplicate_div = document.getElementById('duplicate_1');
function addRecord() {
var clone = duplicate_div.cloneNode(true);
clone.id = "duplicate_" + ++count;
duplicate_div.parentNode.append(clone);
var cloneNode = document.getElementById(clone.id).children[0];
$(clone).find("*[id]").each(function() {
$(this).val('');
var tID = $(this).attr("id");
var idArray = tID.split("_");
var idArrayLength = idArray.length;
var newId = tID.replace(idArray[idArrayLength - 1], count);
$(this).attr('id', newId);
});
}
$(document).ready(function() {
$("#item_" + count).change(function() {
var val = $(this).val();
if (val == "shirt") {
$("#color_" + count).html("<option>Black</option> <option>Gray</option>");
} else if (val == "pants") {
$("#color_" + count).html("<option>Blue</option> <option>Brown</option>");
} else if (val == "shoe") {
$("#color_" + count).html("<option>White</option> <option>Red</option>");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="select-form">
<div class="duplicate" id="duplicate_1">
<br>
<label>item</label>
<select id="item_1">
<option value="template" disabled selected></option>
<option value="shirt">Shirt</option>
<option value="pants">Pants</option>
<option value="shoe">Shoe</option>
</select>
<label>color</label>
<select id="color_1">
<option disabled selected>Select item first</option>
</select>
</div>
</form>
<br><br>
<button type="button" id="add-button" onclick="addRecord()">add</button>
Since you've imported jQuery into the project, I suggest you fully use it.
It's recommended to use jQuery's .on method instead of onclick attribute.
The change event will not work on the dynamically created elements.
You should instead use "event delegation".
Last but not least, you can remove the ids if they serve as selectors. You can use jQuery to easily transverse the DOM
Try this
$(document).ready(function() {
var $cloned = $('.duplicate').first().clone(true);
var $container = $('.select-form');
$('#add-button').click(function() {
$container.append($cloned.clone());
})
$('.select-form').on('change', '.item', function() {
var val = $(this).val();
var $color = $(this).closest('.duplicate').find('.color');
if (val == "shirt") {
$color.html("<option>Black</option> <option>Gray</option>");
} else if (val == "pants") {
$color.html("<option>Blue</option> <option>Brown</option>");
} else if (val == "shoe") {
$color.html("<option>White</option> <option>Red</option>");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="select-form">
<div class="duplicate">
<br>
<label>item</label>
<select class="item">
<option value="template" disabled selected></option>
<option value="shirt">Shirt</option>
<option value="pants">Pants</option>
<option value="shoe">Shoe</option>
</select>
<label>color</label>
<select class="color">
<option disabled selected>Select item first</option>
</select>
</div>
</form>
<br><br>
<button type="button" id="add-button">add</button>

how to onchange input value after selected dropdown

I want to change the input value when the user select data on the input dropdown. The first thing to do after looping and setting data value in the html dropdown is to check whether the option is selected or not, but I am confused about how to check it, so I just checked by dropdown id but it didn't work, so how to check data input if selected?
this is my ajax code to return data dropdown for the first time:
$.ajax({
url : "{{ url('web/city/getdatacity') }}",
type : "POST",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
},
data : {
province_id:id
},
success : function(data){
var html = '';
var postcode = '';
var i;
if(data.length == 0) {
html += '<option value = ""> City not found!</option>';
} else {
for(i=0; i<data.length; i++) {
html += '<option value = "'+ data[i].city_id +'">' + data[i].city_name +'</option>';
if($('#city_id').is(':selected')) { //here the problem when check if option above selected or not
postcode = data[i].postal_code; //input postal code in var postcode if true
}
}
}
$('.city').html(html);
$('#postalcode').val(postcode); //auto fill postal code value from postcode
})
});
Instead of using selected or onchange try something like this with your options attribute.
Look at the oninput
<label for="cars">Choose a car:</label>
<select id="cars" name="cars" oninput="console.log(this.value)">
<option value="volvo">Volvo XC90</option>
<option value="saab">Saab 95</option>
<option value="mercedes">Mercedes SLK</option>
<option value="audi">Audi TT</option>
</select>

Retaining default values for both dropdown values after executing ng-change on first dropdown list to get option values for second dropdown

Only the hospital dropdown code is displaying "HOSPITAL 1" in the dropdown display but the ward dropdown code is not displaying "WARD 1" like it should when I refresh the page.
Everytime I refresh the page it will call ng-change from hospital dropdown and retrieve the respective wards according to the hospital selected which is "HOSP1" but the display for the ward dropdown will be set as "Please Select Ward" instead of "WARD1".
I tried to retain the old wardCode == 'WARD1' like I did for hosp in the controller but to no avail as the value changes after the ng-change is fired.
Does anyone know if I can use angular copy in this scenario?
HTML
<div class="form-group">
<label class="control-label">Hosp</label>
<select ng-model='search.hosp'select-options=" {{hosp}}" select-data="
{{search.hospCode}}" ng-change="wardDropdown()" id="hospCode"
name="hospCode">
<option value="">Please Select Hosp</option>
<option ng-repeat="obj in hosp" value="{{obj.VALUE}}">{{obj.LABEL}}
</option>
</select>
</div>
<div class="form-group">
<label class="control-label">Ward</label>
<select ng-model='search.wardCode' select-options="{{wards}}"
select-data="{{search.wards}}" id="wardCode" name="wardCode">
<option value="">Please Select Ward</option>
<option ng-repeat="obj in wards" value="{{obj.VALUE}}">
{{obj.LABEL}}
</option>
</select>
</div>
JS
$scope.search = {}
$scope.search.hosp = 'HOSP1';
$scope.search.wardCode = 'WARD1';
//Start, hosp dropdown list
$scope.hosp = function () {
var url = /searchHosp',
method = methodType.POST,
functionName = 'searchHosp',
params = {};
if (data.status == messageStatus.success) {
$scope.hospitals = data.result;
} else {
$scope.errorMessage = promptMessage.searchRecordFailure;
}
},
function (error) {
$scope.errorMessage = promptMessage.exception;
)
};
$scope.hospitalList();
//End, hosp dropdown
//Start, ward dropdown
$scope.wardDropdown = function () {
var url = searchWard,
method = methodType.POST,
functionName = 'searchWard',
params = {};
params.hosp = $scope.search.hosp;
if (data.status == messageStatus.success) {
$scope.wards = data.result;
}
}
else {
$scope.errorMessage = promptMessage.searchRecordFailure;
}
$scope.wardDropdown();
//End, ward dropdown list

HTML Select Option : How to Add Another Line of Option

Is it possible to use javascript to modify a select option and add another line of option?
Like this:
<select>
<option value ="1">1</option>
<option value ="2">2</option>
<option value ="3">3</option>
</select>
into this :
<select>
<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>
</select>
If it is possible to do so, can someone tell me how?
select = document.getElementById('selectElementId');
var opt = document.createElement('option');
opt.value = 4;
opt.innerHTML = 4;
select.appendChild(opt);
You can do it in a loop as well:
http://jsfiddle.net/wdmz3cdv/
var min = 4,
max = 5,
select = document.getElementById('selectElementId');
for (var i = min; i<=max; i++){
var opt = document.createElement('option');
opt.value = i;
opt.innerHTML = i;
select.appendChild(opt);
}
In jquery you can do like this...
$('#selectID').append($('<option>', {
value: 1,
text: 'New Option' }));
and in javascript you can do like this..
var x = document.getElementById("selectId");
var option = document.createElement("option");
option.text = "New Option";
option.value = "Your Value";
x.add(option);
give the select an ID
use JavaScript
One way is like this:
var sel = document.getElementById("mySel");
sel.options[sel.options.length]=new Option(4,4); //adds <option value="4">4</option> to end
You can write something like:
var option = document.createElement("option");
option.text = "Text";
option.value = "myvalue";
var select = document.getElementById("id-to-my-select-box");
select.appendChild(option);
Source

Html select keystroke timeout

I'm creating a standard html select dropdown with a hundred or so entries. My users would like to be able to type in the value to get to the proper selection faster. While this is supported natively, the keystroke timeout is very quick, so if you don't type the string quickly, you end up with the wrong selection. Is there a way to increase the timeout? Or has anyone written code to do this manually?
Here's a jsFiddle to illustrate the issues. JsFiddle
label for="title">Choose your poison</label>
<select id="title" name="title">
<option value="Cider" selected>Apple Cider</option>
<option value="Juice">Apple Juice</option>
<option value="Curacao">Curacao</option>
<option value="Jack">Jack's Hard Cider</option>
<option value="Jake">Jake's Hard Cider</option>
<option value="James">James' Hard Cider</option>
<option value="Jamison">Jamison Irish Whiskey</option>
<option value="Kool">Kool Ade</option>
<option value="Lemonade">Lemonade</option>
<option value="Prune">Prune Juice</option>
</select>
Try selecting the Jack's or Jake's by slowly typing and see if you end up selecting Curacao or Kool Ade.
You could use a <datalist> instead. It's supported in IE10 and higher. MDN Page
DEMO
<label for="poison">Choose your poison</label>
<input id="poison" name="poison" list="poisons" />
<datalist id="poisons">
<option value="Cider" selected>Apple Cider</option>
<option value="Juice">Apple Juice</option>
<option value="Curacao">Curacao</option>
<option value="Jack">Jack's Hard Cider</option>
<option value="Jake">Jake's Hard Cider</option>
<option value="James">James' Hard Cider</option>
<option value="Jamison">Jamison Irish Whiskey</option>
<option value="Kool">Kool Ade</option>
<option value="Lemonade">Lemonade</option>
<option value="Prune">Prune Juice</option>
</datalist>
If you have a small number of entries, the answer using datalist is fantastic. However, my users were using lists that had over a hundred entries and the datalist won't scroll. So, I built a utility class for use in a few places.
To use it, create the listFilter and initialize it with your list of choices. Then hook up keyUp and keyPress and use the return values. The class uses a 2-second time out and actually filters the answers. Using the delete key, you can clear the filtering. Be sure to check for a null return value in the keyUp, since that only handles the delete key. Note that the dropdown must be unexpanded for you to get the key events to work.
var listFilter = {
originalListToHold: [],
time1: 1,
search: "",
initialize: function(originalListToCopy) {
this.originalListToHold = [];
for (var i = 0; i < originalListToCopy.length; i++) {
this.originalListToHold[i] = originalListToCopy[i];
}
},
isInOriginalList: function(member) {
return this.originalListToHold.indexOf(member) > 1;
},
keyUpEvent: function(event) {
var keyCode = event.keyCode;
if (keyCode == 46) {
var filtered = this.filterList("");
this.search = "";
event.stopPropagation();
return filtered;
} else {
return null;
}
},
keyPressEvent: function(event) {
//The delete key will reset the list. See the key up event above.
var val = String.fromCharCode(event.which).toUpperCase();
var timenow = event.timeStamp;
var timeDiff = timenow - this.time1;
if (!isNaN(timeDiff)) {
//If the time difference is < 2 seconds (2000 ms), then we
//will search the options.
if (timeDiff > 2000) {
//Reset the search string
this.search = "" + val;
} else {
this.search = this.search + val;
}
} else {
this.search = "" + val;
}
this.time1 = timenow;
//Now, let's filter the options by the search string.
var filtered = this.filterList(this.search);
event.stopPropagation();
return filtered;
},
filterList: function(filter) {
var newList = [];
for (var i = 0; i < this.originalListToHold.length; i++) {
if (this.originalListToHold[i].indexOf(filter) > -1) {
newList.push(this.originalListToHold[i]);
}
}
return newList;
}
}