For some reason I am not able to get Selectize.js working on my project at all. I have checked all the jQuery links and they seem to be fine, but when I view the page all I get is a simple HTML drop down menu. I can't type directly into the box and there is no option to auto complete or add a new entry as at http://selectize.github.io/selectize.js/ (Single Item Select sub heading)
I have tried to copy the simplest example from the Selectize site but even that doesn't work. I can get a simple 'alert' pop up box to work so I know that jQuery is working. The file address selectize/selectize.min.js is also correct.
I would appreciate any help.
HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:400,300,600,700">
<link rel="stylesheet" href="selectize/normalize.css">
<link rel="stylesheet" href="selectize/styles.css">
<link rel="stylesheet" href="selectize/default.css" data-theme="default">
<!--[if IE 8]><script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/2.0.8/es5-shim.min.js"></script><![endif]-->
<script type="text/javascript" src="selectize/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="selectize/selectize.min.js"></script>
<script type="text/javascript" src="selectize/myjquery.js"></script>
</head>
<body>
<section class="demo" id="demo-single-item-select">
<div class="header">
Single Item Select
</div>
<div class="sandbox">
<label for="select-beast">Beast:</label>
<select id="select-beast" class="demo-default" placeholder="Select a person...">
<option value="">Select a person...</option>
<option value="1">Chuck Testa</option>
<option value="4">Sage Cattabriga-Alosa</option>
<option value="3">Nikola Tesla</option>
</select>
</div>
<div class="description">
The most vanilla of examples.
</div>
</section>
</body>
</html>
myjQuery:
$(document).ready(function() {
//alert('Ahoy hoy');
$('#select-beast').selectize({
create: true,
sortField: 'text',
searchField: 'item',
create: function(input) {
return {
value: input,
text: input
}
}
});
});
<option value="">Select a person...</option>
remove value attribute or give it any name
Related
I need to highlight both cars and vans, the below function will highlight for non multiple select2 list, I need to highlight multiple and non multiple selects.
<!DOCTYPE html>
<html>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<select name="cars" id="cars" multiple>
<option value="volvo">Cars</option>
</select>
<select name="vans" id="vans">
<option value="volvo">Vans</option>
</select>
<script>
function highlightSelect2(type, selector) {
$(type+'select2-'+ selector +'-container').effect("highlight", {
color: '#f88'
}, 10000);
}
$(document).ready(function() {
$('#cars').select2();
$('#vans').select2();
highlightSelect2("#","cars")
highlightSelect2("#","vans")
});
</script>
</body>
</html>
You can define the actual select2 classes which will be always same in the library and never changes.
Firstly, check if the select2 is multiple or not. And call your highlightSelect2 function accordingly by passing true for multiple select highlight else and for normal __rendered select2 options.
Add a class to your html selects and check if they have prop of multiple and call select2 for highlighting that html select.
Working Demo:
function highlightSelect2(isMultiple = null) {
//is multiple
var isWhat = isMultiple ? '--multiple' : '__rendered'
//highlight
$('.select2-selection' + isWhat).effect("highlight", {
color: '#f88'
}, 10000);
}
$(document).ready(function() {
//initilize select2
$('.mySelect').select2();
//check each and highlight
$('.mySelect').each(function(index, element) {
//check if its muliple
let prop = $(element).prop('multiple')
//call functions
prop ? highlightSelect2(prop) : highlightSelect2()
})
});
<!DOCTYPE html>
<html>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<select name="cars" class="mySelect" id="cars" multiple>
<option value="volvo">Cars</option>
</select>
<select name="vans" class="mySelect" id="vans">
<option value="volvo">Vans</option>
</select>
</body>
</html>
I need to highlight only the #vans and not the #cars
Sometimes #vans can be multiple and sometimes it can be a non multiple too. However I must be able to specifically pass the ID selector to highlight the select. Here is the code below from Highlight a select2 using highlight method and using css selector answer
<!DOCTYPE html>
<html>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<select name="cars" class="mySelect" id="cars" multiple>
<option value="volvo">Cars</option>
</select>
<select name="vans" class="mySelect" id="vans">
<option value="volvo">Vans</option>
</select>
<script>
function highlightSelect2(selector, isMultiple) {
var isWhat = isMultiple ? '--multiple' : '__rendered'
$('.select2-selection' + isWhat).effect("highlight", {
color: '#f88'
}, 10000);
}
$(document).ready(function() {
//initilize select2
$('.mySelect').select2();
$('.mySelect').each(function(index, element) {
let prop = $(element).prop('multiple')
prop ? highlightSelect2("#vans",prop) : highlightSelect2("#vans")
})
});
</script>
</body>
</html>
If you know that only the select#vans needs highlighting, you don't need to iterate over all Select2 jQuery items. Additionally, your highlightSelect2 isn't using the selector you've passed in.
Using your code sample, I've modified it so that only the #vans element will be highlighted by:
not iterating over all select2 elements (using .each)
This lets you only highlight the #vans, directly
Modifying highlightSelect2 to use the passed-in selector
Removing isMultiple logic — it wasn't necessary
<!DOCTYPE html>
<html>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<select name="cars" class="mySelect" id="cars" multiple>
<option value="volvo">Cars</option>
</select>
<select name="vans" class="mySelect" id="vans">
<option value="volvo">Vans</option>
</select>
<script>
function highlightSelect2(selector) {
$(selector)
.next('.select2-container')
.find(".select2-selection")
.effect("highlight", {
color: '#f88'
}, 10000);
}
$(document).ready(function() {
//initilize select2
$('.mySelect').select2( { width: "25%" });
// highlight the #vans select2
highlightSelect2("#vans");
});
</script>
</body>
Run the code snippet and you'll see it works as you expect for your specific example.
I am trying to implement an autocomplete dropdown menu on our wordpress site. The functionality should be as follows:
Input box where user can start typing, then once they make a selection from the list, specific content is displayed.
I have it figured out with the functionality of returning the content related to the user selection. (code below)
<script>
jQuery(function($){
$('.group').hide();
$('#option0').show();
$('#selectMe').change(function () {
$('.group').hide();
$('#'+$(this).val()).show();
})
});
</script>
<select id="selectMe" class=".dropdown">
<option value="option0">Select Your Brand</option>
<option value="option1">Brand A</option>
<option value="option2">Brand B</option>
<option value="option3">Brand C</option>
<option value="option4">Brand D</option>
</select>
<div id="results">
<div id="option0" class="group">
Generic content as a placeholder until selection is made.
</div>
<div id="option1" class="group">
Content related to Brand A.
</div>
<div id="option2" class="group">
Content related to Brand B.
</div>
<div id="option3" class="group">
Content related to Brand C.
</div>
<div id="option4" class="group">
Content related to Brand D.
</div>
</div>
I'm brand new to any type of javascript or jquery and I am unsure how to make it so they can start typing a word in an input field to show available selections rather than scrolling through a long list in a dropdown menu to find to what they're looking for. (there will be over 200 possible selections in the dropdown)
UPDATE:
I've figured out the autocomplete part by using jquery autocomplete ui
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
var availableTags = [
"Burger King",
"Chick-Fil-A",
"Chipotle Mexican Grill",
"Dunkin'",
"McDonald's",
"Panera Bread",
"Sonic",
"Subway",
"Starbucks",
"Wendy's"
];
$( "#tags" ).autocomplete({
source: availableTags
});
} );
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Search your brand: </label><br><br>
<input id="tags" placeholder="Start typing...">
</div>
<div class="results">
This is where I would want the content based on user selection to be shown.
</div>
</body>
</html>
I went through the autocomplete events but don't understand where I'd put any additional code to show content related to what the user selects in another div.
Any help with this coding would be appreciated!
I want to implement drop-down list with multiple group selection checkbox. There are a lot of options,so I use mysql to save them. How could i get data from mysql to use as option.I want to use php to retrieve data and they must be hierarchical tree structure.
I use this sample code, but option value is directly entered. How to make it display herarchical form as image below.
This is the code:
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-
typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
/>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-
multiselect/0.9.13/js/bootstrap-multiselect.js"></script>
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-
multiselect/0.9.13/css/bootstrap-multiselect.css" />
</head>
<body>
<div class="container" style="width:400px;">
<form method="post" id="framework_form">
<div class="form-group">
<label>Select which Framework you have knowledge</label>
<select id="framework" name="framework[]" multiple class="form-control">
<option value="Codeigniter">Codeigniter</option>
<option value="CakePHP">CakePHP</option>
<option value="Laravel">Laravel</option>
<option value="YII">YII</option>
<option value="Zend">Zend</option>
<option value="Symfony">Symfony</option>
<option value="Phalcon">Phalcon</option>
<option value="Slim">Slim</option>
</select>
</div>
</form>
<br />
</div>
</body>
</html>
<script>
$(document).ready(function () {
$('#framework').multiselect({
nonSelectedText: 'Thêm khách mời',
enableFiltering: true,
enableCaseInsensitiveFiltering: true,
buttonWidth: '400px'
});
});
</script>
Sorry for my bad English and Thanks for reading.
I'm trying to make a dynamic dropdown selection using this theme with Javascript. The <option> elements are taken from an array in a text file using AJAX and inserted in the <select> dropdown:
<head>
<script src="jquery-2.1.4.js"></script>
<link rel="stylesheet" type="text/css" href="css/normalize.css" />
<link rel="stylesheet" type="text/css" href="css/demo.css" />
<link rel="stylesheet" type="text/css" href="css/cs-select.css" />
<link rel="stylesheet" type="text/css" href="css/cs-skin-border.css" />
</head>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url: "http://localhost/array.txt",
beforeSend: function(xhr) {
xhr.overrideMimeType("text/plain; charset=x-user-defined");
}
})
.done(function(data) {
var myArray = JSON.parse(data);
var output = [];
$.each(myArray, function(key, value)
{
output.push('<option value="' + value + '">'+ value +'</option>');
});
$('#myselect').html(output.join(''));
});
</script>
And here's the HTML part:
<body>
<form id="myform" action="select.php">
<input id="myinput" name="owncategory" type="text"> <br><br>
<div>
<select class="cs-select cs-skin-border" id="myselect">
<option value="" disabled selected>Select a category</option>
</select>
</div>
<div><input id="mysubmit" type="submit" value="Submit"></div>
</form>
<script src="js/classie.js"></script>
<script src="js/selectFx.js"></script>
<script>
(function() {
new SelectFx($(".cs-select").get(0));
})();
</script>
</body>
The problem is, the dropdown selection (which is now stylized by the script) doesn't show the added <option> elements, even though when the page is inspected on a browser, they're there. I don't know how to show them as part of the stylized dropdown. Any ideas?
EDIT: I just figured it out, kind of. So instead of inserting it on the select element with the #myselect id:
$('#myselect').html(output.join(''));
I inserted it on a <div> element that shows up when the page loads:
$('.cs-options').html(output.join(''));
And now I get this: http://i.imgur.com/KMEurVP.png
But now my current problem is that they're un-selectable.
Work for me.
<li data-option="" data-value="therock" class=""><span>The rock</span></li>