I used select2 to produce my select option list like the snippet below.
$(function(){
$(".select2").select2();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/css/select2.min.css" rel="stylesheet"/>
<div class="form-group">
<label>Minimal</label>
<select class="form-control select2" style="width: 100%;">
<option selected="selected">Alabama</option>
<option>Alaska</option>
<option>California</option>
<option>Delaware</option>
<option>Tennessee</option>
<option>Texas</option>
<option>Washington</option>
</select>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.min.js"></script>
What I want is, a subtext below the main text that searchable too. when i want to search the option with value Alabama, I just need to type Okay1, but in the other hand I want to search with value Alabama only. Are there any plugins other than select2 option library that support this kind of behavior ? ( Below is the image that i expected for the result )
I have tried like the code below, but it didn't work that well :
<div class="form-group">
<label>Minimal</label>
<select class="form-control select2" style="width: 100%;">
<option selected="selected">Alabama  <h4 style="color: grey">Okay1</h4></option>
<option>Alaska  <h4 style="color: grey">Okay2</h4></option>
<option>California  <h4 style="color: grey">Okay2</h4></option>
<option>Delaware  <h4 style="color: grey">Okay3</h4></option>
<option>Tennessee  <h4 style="color: grey">Okay4</h4></option>
<option>Texas  <h4 style="color: grey">Okay5</h4></option>
<option>Washington  <h4 style="color: grey">Okay6</h4></option>
</select>
</div>
You could:
encode the secondary property as an attribute of the <option> element
use a custom search function, see matchCustom in snippet below
use a custom formatter function, see formatCustom below
Working example, using state mottos as secondary attribute stored in data-foo, you can search by state name or motto:
$(function(){
$(".select2").select2({
matcher: matchCustom,
templateResult: formatCustom
});
})
function stringMatch(term, candidate) {
return candidate && candidate.toLowerCase().indexOf(term.toLowerCase()) >= 0;
}
function matchCustom(params, data) {
// If there are no search terms, return all of the data
if ($.trim(params.term) === '') {
return data;
}
// Do not display the item if there is no 'text' property
if (typeof data.text === 'undefined') {
return null;
}
// Match text of option
if (stringMatch(params.term, data.text)) {
return data;
}
// Match attribute "data-foo" of option
if (stringMatch(params.term, $(data.element).attr('data-foo'))) {
return data;
}
// Return `null` if the term should not be displayed
return null;
}
function formatCustom(state) {
return $(
'<div><div>' + state.text + '</div><div class="foo">'
+ $(state.element).attr('data-foo')
+ '</div></div>'
);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/css/select2.min.css" rel="stylesheet"/>
<style>
.foo { color: #808080; text-size: smaller; }
</style>
<div class="form-group">
<label>Minimal</label>
<select class="form-control select2" style="width: 100%;">
<option selected="selected" data-foo="We dare to defend our rights">Alabama</option>
<option data-foo="North to the Future">Alaska</option>
<option data-foo="Eureka">California</option>
<option data-foo="Liberty and Independence">Delaware</option>
<option data-foo="Agriculture and Commerce">Tennessee</option>
<option data-foo="Friendship">Texas</option>
<option data-foo="Bye and bye">Washington</option>
</select>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.min.js"></script>
I would use Bootstrap-select. It's a great library and already does every thing you want. No need to reinvent the wheel as they say!
You can search on both the title and description right out the box too.
<div class="container">
<div class="row">
<div class="col-xs-12">
<div class="form-group ">
<label>Minimal</label>
<select class="selectpicker form-control" data-live-search="true" title="Search title or description...">
<option data-subtext="description 1">Alaska</option>
<option data-subtext="description 2">California</option>
<option data-subtext="description 3">Delaware</option>
<option data-subtext="description 4">Tennessee</option>
<option data-subtext="description 5">Texas</option>
<option data-subtext="description 6">Washington</option>
</select>
</div>
</div>
</div>
</div>
CSS
.dropdown-menu > li > a {
font-weight: 700;
padding: 10px 20px;
}
.bootstrap-select.btn-group .dropdown-menu li small {
display: block;
padding: 6px 0 0 0;
font-weight: 100;
}
Here is a fiddle with a little bit of customisation
Related
I would like to create an input field that would create a drop-down list of available values when clicked. After that, the user can select one or more values and those will be added to the field, separated by commas (just like how we add the stackoverflow tags when asking a question). I can create a drop-down list like below, but it only allows me to select one single value at a time.
<div class="form-group">
<select class="form-control" name="year">
<option class="hidden" selected disabled>Please select your year</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</div>
You can listen for change or click events on the select and construct a list of tokens based on what was clicked, or what's currently selected:
const s = document.querySelector('select');
const list = document.querySelector('ul');
s.addEventListener('change', e => {
list.innerHTML = [...e.target.selectedOptions].map(({value}) => `<li>${value}</li>`).join('');
})
:root {
font-family: sans-serif;
}
select {
min-width: 80px;
margin-bottom: 2rem;
}
ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
}
li {
background: skyblue;
color: aliceblue;
border-radius: 4px;
padding: 0.5em 1em;
margin-right: 0.5em;
}
<select multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<ul></ul>
You can use select2 a jquery function
<select class="multiple_state" name="state" multiple>
<option value="0">Rajasthan</option>
<option value="1">Gujarat</option>
</select>
and JQuery code is
$(document).ready(function() {
$('.multiple_state').select2();
});
using this CDN
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css">
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
in your head tag
HI i have a example with me when if select the drop down list - price by recyclables a hidden field apppear but how can i when select the Service charger the hidden field disappear
I have try using .attr to add back but it seems to be not working
$("#select-price-mode").change(function() {
if (this.value === "Price By Recyclables") {
$('.col').removeAttr('hidden');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-row">
<div class="col">
<label for="select-price-mode" class="col-form-label">Price Mode</label>
<select class="select-price-mode custom-select-sm col-10" id="select-price-mode" required>
<option selected disabled value="">Select ....</option>
<option value="Price By Recyclables">Price By Recyclables</option>
<option value="Service Charger">Service Charger</option>
</select>
</div>
<div class="col" hidden>
<label for="select-payment-frequency" class="col-form-label">Payment Frequency</label>
<select class="select-payment-frequency custom-select-sm col-10" id="select-payment-frequency" required>
<option selected disabled value="">Select ....</option>
</select>
</div>
i think this will solve your issue
$("#select-price-mode").change(function() {
if (this.value === "Price By Recyclables") {
$('.col').removeAttr('hidden');
} else {
$('.col').attr('hidden', '');
}
});
On the front-end I'm asking the user 2 questions, the first one visible, the second will be visible depending on the firs answer. But somehow my removeClass is not working as expected (not working at all)
<div class="form-group col-md-4 md-form">
<label for="savingcontratos">Cancelamento de Contratos?</label>
<select class="mdb-select md-form" id="savingcontratos" searchable="Pesquisar">
<option value=""></option>
<option value="0">Sim</option>
<option value="1">Não</option>
</select>
</div>
<div class="form-group col-md-4 md-form invisible" id="divcontratos">
<label for="txtcontratos">Insira os contratos envolvidos</label>
<input type="text" class="form-control" id="txtcontratos" value="0">
</div>
And the functions I'm using are:
$('#savingfinanceiro').on('change',function () {
var component = $("#divfinanceiro");
makevisible($('#savingfinanceiro').val(), component);
});
function makevisible(value,component){
if (value === "0"){
component.removeClass("invisible");
}
}
Any help would be greatly appreciated.
as pointed out in the comments #savingfinanceiro and #divfinanceiro do not exist in your html. You must have overlooked that. In the following snippet I have fixed that. There, #savingfinanceiro, from the <select>, was changed to #savingcontratos and var component = $("#divfinanceiro");, the 'to-hide' <div> was changed to var component = $("#divcontratos");. After applying these changes your code should work as expected.
$('#savingcontratos').on('change', function() {
var component = $("#divcontratos");
makevisible($('#savingcontratos').val(), component);
});
function makevisible(value, component) {
if (value === "0") {
component.removeClass("invisible");
} else {
component.addClass("invisible");
}
}
.invisible {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group col-md-4 md-form">
<label for="savingcontratos">Cancelamento de Contratos?</label>
<select class="mdb-select md-form" id="savingcontratos" searchable="Pesquisar">
<option value=""></option>
<option value="0">Sim</option>
<option value="1">Não</option>
</select>
</div>
<div class="form-group col-md-4 md-form invisible" id="divcontratos">
<label for="txtcontratos">Insira os contratos envolvidos</label>
<input type="text" class="form-control" id="txtcontratos" value="0">
</div>
Hope this helps!
As of right now, I have two lists of options to choose from for a form in HTML. However, choosing a value in one list would make certain options in the second list impossible. Specifically, the first list is a list of possible units (cm, in, ft.), while the second is a list of locations. Choosing a unit would limit the number of possible locations that would have this unit. Similarly, choosing a country value would limit the number of units that are available. Would it be possible in any way to limit the choices available to the user after they select either a location or a unit?
Unit:
<br>
<select name="unit_input">
<br>
<option selected disabled hidden></option>
<option value="l">League</option>
<option value="m">Mile</option>
<option value="ft">Foot</option>
<option value='m'>Meter</option>
<option value="st">Stage</option>
<option value="km">Kilometer</option>
</select>
<br>
Location:
<br>
<select name="nationality_input">
<br>
<option selected disabled hidden></option>
<option value="italian">Italian</option>
<option value="german">German</option>
<option value="french">French</option>
<option value="hungarian">Hungarian</option>
<option value="british">British</option>
<option value="swiss">Swiss</option>
<option value="spanish">Spanish</option>
</select>
<br>
<br>
Yes, using JavaScript: https://codepen.io/dpamonty/pen/PoqRZQd
(Don't forget to add ID to your drop-down list for the code to work).
<!doctype html>
<html>
<head>
</head>
<body>
Unit:
<br>
<select id="unit_input" name="unit_input" onchange="restrictDropDownLists()">
<br>
<option selected disabled hidden></option>
<option value="l">League</option>
<option value="m">Mile</option>
<option value="ft">Foot</option>
<option value='m'>Meter</option>
<option value="st">Stage</option>
<option value="km">Kilometer</option>
</select>
<br>
<br>
<br>
Location:
<br>
<select id="nationality_input" name="nationality_input" onchange="restrictDropDownLists()">
<br>
<option selected disabled hidden></option>
<option value="italian">Italian</option>
<option value="german">German</option>
<option value="french">French</option>
<option value="hungarian">Hungarian</option>
<option value="british">British</option>
<option value="swiss">Swiss</option>
<option value="spanish">Spanish</option>
</select>
<br>
<br>
<script type="text/javascript">
function restrictDropDownLists(){
var unit = document.getElementById("unit_input");
var nationalilty = document.getElementById("nationality_input");
switch(unit.value){
case "m":
// Options to hide:
nationalilty.options[1].style.display = "none"; // Italian
// etc.
// Options to show:
nationalilty.options[4].style.display = ""; // British
// etc.
break;
// etc.
}
switch(nationalilty.value){
case "british":
// Options to hide:
unit.options[6].style.display = "none"; // Kilometer
// etc.
// Options to show:
unit.options[1].style.display = ""; // League
// etc.
// etc.
}
}
// Restrict on the page load as well:
restrictDropDownLists();
</script>
</body>
</html>
Theoretically you should be able to do this with pure CSS with something like:
#second option { display: none; }
#first:has(> option#a:checked) ~ #second option.a { display: block; }
#first:has(> option#b:checked) ~ #second option.b { display: block; }
#first:has(> option#c:checked) ~ #second option.c { display: block; }
<select id="first">
<option id="a">a</option>
<option id="b">b</option>
<option id="c">c</option>
</select>
<select id="second">
<option class="a b c">Shown for all options</option>
<option class="a">Shown when a selected</option>
<option class="c">Shown when c selected</option>
</select>
However, nothing supports :has() yet. With a change of implementation, you can do something similar in pure CSS. Although, it is far far better to go the JavaScript route; as the following has a number of caveats:
label:after { content: ''; display: block; }
input[name=second] { display: none; }
input[name=second] + label { display: none; }
#a:checked ~ input[name=second].a { display: inline; }
#a:checked ~ input[name=second].a + label { display: inline; }
#b:checked ~ input[name=second].b { display: inline; }
#b:checked ~ input[name=second].b + label { display: inline; }
#c:checked ~ input[name=second].c { display: inline; }
#c:checked ~ input[name=second].c + label { display: inline; }
<h4>First</h4>
<input id="a" type="radio" name="first" /><label>A</label>
<input id="b" type="radio" name="first" /><label>B</label>
<input id="c" type="radio" name="first" /><label>C</label>
<h4>Second</h4>
<input id="d" type="radio" name="second" class="a b c" /><label>Shown for all options</label>
<input id="e" type="radio" name="second" class="a" /><label>Shown when a is selected</label>
<input id="f" type="radio" name="second" class="c" /><label>Shown when c is selected</label>
The major issue with the above is that in order to rely on the ~ selector, all the inputs have to be a sibling. So this greatly impacts your layout, and what you can actually achieve. Once :has() is supported, pure CSS options will exist, but even then you typically want more logical handling than CSS can offer i.e. how do you allow a user to leave and return to the same point in the form? Or, what happens to selected option's value when it disappears? This is where JavaScript will definitely win out.
Forms are a perfect example of how building a UI (user interface) based upon a state tree is a good idea. This tends to be the focus of frameworks like React, or Vue. Where, depending on the current "state" (an object tree that describes chosen options) the UI just "reacts" and hides/shows conditional elements. To read about this from a pure "state" perspective, looking into Redux or similar technologies as a basis for what you are building could be good idea.
I was wondering how to add a class to a div if the option has the value selected. For example I'm making a website for my cousin's business, we want an easy way to shuffle through menus.
<code>
<div id="select"><form>
Menu:
<select name="select" class="form-control">
<option value="drinks">Drinks</option>
<option value="breakfast">Breakfast</option>
</select>
<input type="submit" value="Submit">
</form>
</code>
I would like if the value is breakfast to hide the div drinks and show the div breakfast, and also have it reversible. Thank you here's my code on JSFiddle http://jsfiddle.net/t5R9s/
Here it is
$(document).ready(function() {
$(".form-control").change(function() {
if($(this).val() == "drinks") {
$("#drinks").show();
$("#breakfast").hide();
} else {
$("#drinks").hide();
$("#breakfast").show();
}
});
});
Here is the FIDDLE.
You can use javascript. Do you want to do it when you hit submit or just after you make the selection?
[
Fiddle
]
<script type="text/javascript">
function changeClass(){
var myVariable = document.getElementById('control').value;
if(myVariable == "drinks"){
document.getElementById('drinks').className = "";
document.getElementById('breakfast').className = "hide";
}
else if(myVariable == "breakfast"){
document.getElementById('drinks').className = "hide";
document.getElementById('breakfast').className = "";
}
}
</script>
<div id="select">
<form>Menu:
<select name="select" class="form-control" id="control" onChange="changeClass()">
<option value="drinks">Drinks</option>
<option value="breakfast">Breakfast</option>
</select>
</form>
</div>
<div id="drinks">
<h2 align="center">Drinks</h2>
<table width="888" border="" cellpadding="9" class="">..</table>
</div>
<!--Breakfast table-->
<div id="breakfast" class="hide">
<h2 align="center">Breakfast</h2>
<table width="888" border="" cellpadding="9" class="">..</table>
</div>