select2 js plugin is not working - javascript

I just started using select2 jquery plugin . I am trying it on codepan . I added all cdn link (css,js, jquery etc) .But my select items do not change yet . here is my code :
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta -->
<meta charset="UTF-8" />
<title>My New Pen!</title>
<!-- Styles -->
<link rel="stylesheet" href="styles/index.processed.css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/css/select2.min.css" rel="stylesheet" />
</head>
<body>
<div style="width:600px;">
<form action="post">
<select name="" id="myselection">
<option value="dhaka"> Dhaka </option>
<option value="braishal"> Barishal </option>
<option value="Khulna"> Khulna </option>
<option value="rajshahi"> Rajshahi </option>
<option value="dinajpur"> Dinajpur </option>
</select>
</form>
</div>
<!-- Scripts -->
<script src="scripts/index.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#myselection').select2();
});
</script>
</body>
</html>

JQuery must be imported before Select2 script for it to work.
So the correct import order should be:
jQuery
Select2
Page specific Javascript
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta -->
<meta charset="UTF-8" />
<title>My New Pen!</title>
<!-- Styles -->
<link rel="stylesheet" href="styles/index.processed.css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/css/select2.min.css" rel="stylesheet" />
</head>
<body>
<div style="width:600px;">
<form action="post">
<select name="" id="myselection">
<option value="dhaka"> Dhaka </option>
<option value="braishal"> Barishal </option>
<option value="Khulna"> Khulna </option>
<option value="rajshahi"> Rajshahi </option>
<option value="dinajpur"> Dinajpur </option>
</select>
</form>
</div>
<!-- Scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.min.js">
</script>
<script src="scripts/index.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#myselection').select2();
});
</script>
</body>
</html>

Related

Hide Arrow icon in selectize.js

I'm using the selectize.js package in my application (https://github.com/selectize/selectize.js) and i'm trying to hide the cursor.
Bellow is a code as example:
$('#select-beast-single-disabled').selectize({
create: true,
sortField: {field: 'text'}
});
<html>
<head>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/js/standalone/selectize.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.css" />
<div class="demo">
<div class="control-group">
<select id="select-beast-single-disabled" class="demo-default" placeholder="Select a person...">
<option value="">Select a person...</option>
<option value="1">Arnold</option>
<option value="2" selected>Nikola Tesla</option>
</select>
</div>
</div>
</body>
</head>
</html>
Is there any workaround for it? Thank you in advance !
I believe this solves your problem
therefore, two possibilities:
Set the option maxItems with '1' as a string, in order to skip the condition which insert a different css class in the input.
like this:
$('#select-beast-single-disabled').selectize({
create: true,
sortField: {field: 'text'},
maxItems: '1'
});
<html>
<head>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/js/standalone/selectize.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.css" />
<div class="demo">
<div class="control-group">
<select id="select-beast-single-disabled" class="demo-default" placeholder="Select a person...">
<option value="">Select a person...</option>
<option value="1">Arnold</option>
<option value="2" selected>Nikola Tesla</option>
</select>
</div>
</div>
</body>
</head>
</html>
Remove the arrow from the css.
like this:
$('#select-beast-single-disabled').selectize({
create: true,
sortField: {field: 'text'}
});
<html>
<head>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/js/standalone/selectize.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.6/css/selectize.css" />
<div class="demo">
<div class="control-group">
<select id="select-beast-single-disabled" class="demo-default" placeholder="Select a person...">
<option value="">Select a person...</option>
<option value="1">Arnold</option>
<option value="2" selected>Nikola Tesla</option>
</select>
</div>
</div>
<style>
.selectize-control.single .selectize-input:after{
display:none;
}
</style>
</body>
</head>
</html>

How Create multi select box to create tags

I Want have a multi select box to create tags,I used this code and download select2.min.css and select2.min.js from https://github.com/select2/select2 and copied in css and js file in project,
My code in html file is here:
<link rel="stylesheet"href="https:
//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link type="text/css" rel="stylesheet" href="
{{asset('css/select2.min.css')}}">
<script type="text/javascript" src="{{asset('js/select2.full.min.js')}}">
</script>
<script type="text/javascript"
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
</script>
<script type="text/javascript" src="{{asset('js/select2.min.js')}}">
</script>
<div class="form-group">
<label class="control-label col-sm-2" for="tags">tags:</label>
<select class="form-control select2-multi" name="tags"
multiple="multiple">
#foreach($tags as $tag)
<option value='{{$tag->id}}'>{{$tag->name}}</option>
#endforeach
</select>
</div>
<script>
$('.select2-multi').select2();
</script>
But this Code didn't Work for me,How I Can fixed it?
Image of this code is here:
You need to include jQuery in your project as $ will be undefined. Below is the sample:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<div class="form-group">
<label class="control-label col-sm-2" for="tags">tags:</label>
<select class="form-control select2-multi" name="tags" multiple="multiple">
<option value='tag1'>tag 1</option>
<option value='tag2'>tag 2</option>
<option value='tag3'>tag 3</option>
</select>
</div>
<script>
$('.select2-multi').select2();
</script>
But it will always be a good practice to include your JS code on DOM ready like:
$(function(){
$('.select2-multi').select2();
});

Bootstrap select not working when template that contains select box is included via ng-include

This is my index.html page.
<head>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css">
<link rel="stylesheet" href="css/header.css">
<link rel="stylesheet" href="css/homepage_search_styles.css">
<script src="assets/js/jquery-2.1.0.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="js/controller.js"></script>
<script src="js/quickview.js"></script>
</head>
<body ng-app="myApp">
<div ng-include="'testsearch.html'"></div>
</body>
This is testsearch.html
<div ng-controller="exampleController">
<div>
Blah blah blah
</div>
<select name="model" id="model" class="selectpicker" data-live-search="true" multiple ng-model="sel" title="Any">
<option data-tokens="Restaurant">Restaurant </option>
<option data-tokens="Vegetarian">Vegetarian</option>
<option data-tokens="Bar">Bar</option>
<option data-tokens="Night Life">Night Life</option>
<option data-tokens="Breakfast">Breakfast</option>
<option data-tokens="Fast Food">Fast Food</option>
</select>
<div> Blah blah blah</div>
</div>
All the other components except the select box appears when I run index.html.
Also, if I include the testsearch.html template directly in the index.html, selectbox appears.
Can somebody tell me why does it behave that way?

bootstrap-select drop-down get hidden by another drop-down

Why does some parts of the first drop-down menu get hidden by another drop-down menu below in the following example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="">
<meta name="author" content="">
<!-- Note there is no responsive meta tag here -->
<link rel="shortcut icon" href="../../docs-assets/ico/favicon.png">
<title>MarkerDB</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" >
<link rel="stylesheet" type="text/css" href=//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.2/css/bootstrap-select.min.css>
<!-- Custom styles for this template -->
<link href="http://getbootstrap.com/examples/non-responsive/non-responsive.css" rel="stylesheet">
</head>
<body>
<div class="container">
<form class="form-inline" action="/between/" method="get">
<div class="input-group">
<span class="input-group-addon">TestA</span>
<select name="tests" class="selectpicker form-control" data-live-search="true" title="Plese select a test ...">
<option selected value="1">Test1</option>
<option selected value="2">Test2</option>
</select>
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Search!</button>
</span>
</div>
</form>
<hr>
<form class="form-horizontal" action="/compare/" method="get" role="form">
<div class="input-group">
<span class="input-group-addon">TestB:</span>
<select name='cultnames' class="selectpicker show-menu-arrow form-control" multiple data-max-options="2" data-live-search="true">
<option value="Test3">Test3</option>
<option value="Test4">Test4</option>
</select>
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Search!</button>
</span>
</div>
</form>
<hr>
<HR>
</div> <!-- /container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.2/js/bootstrap-select.min.js"></script>
<script>
$('.selectpicker').selectpicker();
</script>
</body>
</html>
If you put z-index:0 on the input-group in the 2nd form it works ok.
...
<form class="form-horizontal" action="/compare/" method="get" role="form">
<div class="input-group" style="z-index:0;">
...
http://jsfiddle.net/kme2j8ma/

jqtransform dynamic select option?

i am using jqtransform plugin to create select input, i make some change in js file i am creating a select option instead of li and i create a differente script to get select option innerhtml but when i calling this function on window.onload function is working for not working second time.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="gmapkey" content="" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<title></title>
<link rel="stylesheet" href="jqtransformplugin/jqtransform.css" type="text/css" media="all" />
<link rel="stylesheet" href="demo.css" type="text/css" media="all" />
<script type="text/javascript" src="requiered/jquery.js" ></script>
<script type="text/javascript" src="jqtransformplugin/jquery.jqtransform.js" ></script>
<script language="javascript">
$(function(){
$('form').jqTransform({imgPath:'jqtransformplugin/img/'});
});
</script>
<script type="text/javascript">
function change () {
var jAm= document.getElementById('sltnew');
var jM= jAm.options[jAm.selectedIndex].text;
document.getElementById('catch').innerHTML=jM;
}
document.ajay.sltnew.onchange=change;
</script>
</head>
<body>
<form action="post.php" method="POST" name="ajay">
<div class="rowElem"><label>Input Text:</label><input type="text" name="inputtext"/></div>
<div class="rowElem"><label>Input Password:</label><input type="password" /></div>
<div class="rowElem"><label>Checkbox: </label><input type="checkbox" name="chbox" id=""></div>
<div class="rowElem"><label>Radio :</label>
<input type="radio" id="" name="question" value="oui" checked ><label>oui</label>
<input type="radio" id="" name="question" value="non" ><label>non</label></div>
<div class="rowElem"><label>Textarea :</label> <textarea cols="40" rows="12" name="mytext"></textarea></div>
<div class="rowElem">
<label>Select :</label>
<select name="select">
<option value="">1</option>
<option value="opt1"></option>
</select>
</div>
<div class="rowElem" style="float:left; width:500px; position:relative">
<label>Select Redimentionné :</label>
<select name="sltnew" id="sltnew" onchange="change ()">
<option value="opt1">Select states</option>
<option value="opt2">Option 2</option>
<option value="opt3">Option 3</option>
<option value="opt4">Option 4</option>
<option value="opt5">Option 5</option>
<option value="opt6">Option 6</option>
<option value="opt7">Option 7</option>
<option value="opt8">Option 8</option>
</select>
<div id="catch">select city</div>
</div>
<div class="rowElem"><label>Submit button:</label><input type="submit" value="Envoyer" /></div>
<div class="rowElem"><label>Reset button:</label><input type="reset" value="Annuler" /></div>
<div class="rowElem"><label>Input button:</label><input type="button" value="bouton" /></div>
</form>
</body>
</html>
change in jqtransform file what i made.
$wrapper.prepend('<div><span></span></div><select id="slt"></select>');
var $ul = $('select', $wrapper).css('width',$select.width()).hide();
/* Now we add the options */
$('option', this).each(function(i){
var oLi = $('<option>'+ $(this).html() +'</option>');
$ul.append(oLi);
});
just try this
http://www.pkoretic.net/csTransPie

Categories