I want to generate select2 on button click - jquery-select2

I'm adding select2 element like this
//HTML Code
<html>
<select id="select2Basic" class="select2 form-select form-select-lg">
<option value="1"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 2 </option>
</select>
<select id="select3Basic" class="select2 form-select form-select-lg">
<option value="4"> 4 </option>
<option value="5"> 5 </option>
<option value="6"> 6 </option>
</select>
</html>
//Jquery
<script>
$("#select2Basic").select2(); //initialise select2 using id
$("#select3Basic").select2();
</script>
// I can't use same id on multiple element, that wont work.
Now i want a button that generate select2 element when clicked

Related

how can i set a select menu in jquery mobile to the local storage?

I have a select menu in jquery mobile and I want to set it to the local storage and then later to get the values from it. This is so if a user selects a car it will be able to delete the car from the list?
Does anyone know how to accomplish this?
<div data-role="fieldcontain">
<label for="select1">Choose a car to rent:</label>
<select name="select1" id="select1" onchange="selectvalue(this.value);">
<option value="97-113-13">Avenger </option>
<option value="97-225-13">Caravan </option>
<option value="97-112-13">Focus </option>
<option value="97-119-13">Escape</option>
<option value="97-226-13">E 350</option>
<option value="97-227-13">Mustang</option>
<option value=" 97-118-13">Wrangler</option>
<option value="97-223-13">Grand Cherokee</option>
<option value="97-111-13">Picanto</option>
<option value="97-114-13">Galant</option>
<option value="97-115-13">Altima</option>
<option value="97-116-13">Juke</option>
<option value="97-222-13">Corolla</option>
</select>
</div>
DEMO
<div data-role="fieldcontain">
<label for="selCar">Select car:</label>
<select name="selCar" id="selCar">
<option value="97-113-13">Avenger</option>
<option value="97-225-13">Caravan</option>
<option value="97-112-13">Focus</option>
<option value="97-119-13">Escape</option>
<option value="97-226-13">E 350</option>
<option value="97-227-13">Mustang</option>
<option value=" 97-118-13">Wrangler</option>
<option value="97-223-13">Grand Cherokee</option>
<option value="97-111-13">Picanto</option>
<option value="97-114-13">Galant</option>
<option value="97-115-13">Altima</option>
<option value="97-116-13">Juke</option>
<option value="97-222-13">Corolla</option>
</select>
</div>
Each time you submit the form, get the currently selected value from the select menu and save it to localStorage. Each time the page is created, read the value from localStorage and then remove it from the options list:
$(document).on("pagecreate", "#page1", function(){
RemovePreviouslySelected();
$("#btnSubmit").on("click", function(){
var selVal = $("#selCar").val();
localStorage.setItem("selectedCar", selVal);
});
});
function RemovePreviouslySelected(){
var prev = localStorage.getItem("selectedCar");
if (prev){
$("#selCar option").each(function(index){
if ($(this).prop("value") == prev){
$(this).remove();
return;
}
});
}
}

Unable to scroll select options in modal dialog (Chrome)

I have a form within a dialog that has a large number of select option elements. This works fine in IE and FF, but in Chrome, I am unable to scroll through the options. I can select them by selecting an option, then after the options collapse back down, use the arrow keys to go through the list. I am using Chrome Version 32.0.1700.76 and jQuery 1.9.1. I did attempt it using 1.10.2 as well and it did not fix the problem. My fiddle example uses 1.9.1 because selecting 1.10.2 did not allow be to include jQuery UI 1.9.2. You can even see in fiddle that the framework select input is not scrollable. Thanks.
http://jsfiddle.net/4mH8a/19/
HTML:
<form>
<input type="button" value="Open Modal" onclick="$('#divSelect').dialog('open');" />
</form>
<div id="divSelect" style="display:none">
<form>
<select name="lotsofoptions">
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
<option value="4">option 4</option>
<option value="5">option 5</option>
<option value="6">option 6</option>
<option value="7">option 7</option>
<option value="8">option 8</option>
<option value="9">option 9</option>
<option value="10">option 10</option>
<option value="11">option 11</option>
<option value="12">option 12</option>
<option value="13">option 13</option>
<option value="14">option 14</option>
<option value="15">option 15</option>
<option value="16">option 16</option>
<option value="17">option 17</option>
<option value="18">option 18</option>
<option value="19">option 19</option>
<option value="20">option 20</option>
<option value="21">option 21</option>
<option value="22">option 22</option>
<option value="23">option 23</option>
<option value="24">option 24</option>
<option value="25">option 25</option>
<option value="26">option 26</option>
<option value="27">option 27</option>
<option value="28">option 28</option>
<option value="29">option 29</option>
<option value="30">option 30</option>
</select>
</form>
</div>
JS:
$(document).ready(function(){
// Modal dialogs
$('#divSelect').dialog( { autoOpen:false,
modal: true,
title: 'Unable to scroll',
open: function (event, ui) { window.setTimeout(function () {
jQuery(document).unbind('mousedown.dialog-overlay').unbind('mouseup.dialog-overlay'); }, 100);
}
} );
});

How to make a custom select box with filter using jQuery Mobile?

I am having a problem with jQuery Mobile. I want to make a custom select box with search option on the top like in the ul of jQuery mobile. I have made a custom select box using jQuery mobile but cannot add the search bar on it. I want exactly the thing like this:
http://jsfiddle.net/sEMyT/2/
<!DOCTYPE html>
<html>
<head>
<link class="jsbin" href="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.css" rel="stylesheet" type="text/css" />
<script class="jsbin" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script class="jsbin" src="https://github.com/downloads/aliok/jquery-mobile/jquery.mobile_selectmenu_filter_01.js"></script>
<title>JS Bin</title>
</head>
<body>
<div data-role="page">
<div data-role="content">
<h2>Select menu options filtering</h2>
<p>The mobile browsers are limited and sometimes it is annoying to scroll a long list. For this kind of selects, you can use <code>data-filter</code> attribute alongside with <code>data-force-dialog</code> and <code>data-native</code> attributes to have a select menu with search bar filtering its options.</p>
Refresh the selectmenu (forcerebuild) with clearing the filter value
<div data-role="fieldcontain">
<label for="select-choice-12" class="select">Your state:</label>
<select name="select-choice-12" id="select-choice-12" data-native-menu="false" data-force-dialog="true" data-native-menu="false" data-filter="true">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
</div>
You can see the source code at https://github.com/aliok/jquery-mobile/commits/selectmenu-forceDialog/
<br/>
<br/>
Developed by Ali Ok - #aliok_tr
</div>
</div>
</body>
</html>
But the problem with that it is customized with a very old version of jQuery Mobile and I am using the latest version.
You could just follow the example on the JQM demo pages Filter inside Custom Select
I hope you can Use Select2 or Chosen
Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results.
For more details and Demo : check Select2 or Try Chosen
Its implemented in our some projects and its works fine.
check out this in the jquery mobile site:
selectmenu-custom-filter
this is the original jquery mobile solution
this link its the best https://github.com/studiosedition/JQM-Custom-Search-Filtering-Example i tested
recommended by http://www.peachpit.com/articles/article.aspx?p=1929169
other example for custom filter
http://jsfiddle.net/dondavidml/ekbo4uy5/
$("ul").listview('option', 'filterCallback', function (text, searchValue) {
console.log(" searchValue " + searchValue.toUpperCase() + " text " + text.substring(0, searchValue.length).toUpperCase());
if (searchValue.toUpperCase() == (text.substring(0, searchValue.length).toUpperCase())) {
return false;
}
return true; });
be careful, the order matters in the imports like this
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="js/search-filter.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
:D

Drop down popup is not showing using marionette.js and jquery mobile

I am using marionette.js and jquery mobile for my application. Following is the code that I have used in template file for drop down but its not working, I have used "data-native-menu=false" for displaying option in popup
<div id="whatDiv" data-role="fieldcontain">
<label >Main Category</label><br>
<select data-native-menu=false>
<option value=1> Balance </option>
<option value=2> 2 </option>
<option value=3> 3 </option>
<option value=4> 4 </option>
<option value=5> 5 </option>
</select>
</div>
Thanx in advance
i think you want like this , see below jsfiddle links
www.jsfiddle.net/57eJm/

Jquery Mobile - Multiple Select Menu redirecting issue

I am having a problem with Multiple Select Menu in a jquery-mobile app.
Here is my sample Code (just for demo) http://jsfiddle.net/yesvin/aSBCX/
<div data-role="page" id="one">
<div data-role="content">
<label for="sel">Select the Options </label>
<select name="sel" id="sel" data-native-menu="false" multiple="multiple" >
<option value="">Slect the Options</option>
<option value="1">Prod 1</option>
<option value="2">Prod 2</option>
<option value="3">Prod 3</option>
<option value="4">Prod 4</option>
<option value="5">Prod 5</option>
<option value="6">Prod 6</option>
<option value="7">Prod 7</option>
<option value="8">Prod 8</option>
<option value="9">Prod 9</option>
<option value="10">Prod 10</option>
<option value="11">Prod 11</option>
<option value="12">Prod 12</option>
<option value="13">Prod 13</option>
<option value="14">Prod 14</option>
<option value="15">Prod 15</option>
<option value="16">Prod 16</option>
<option value="17">Prod 17</option>
<option value="18">Prod 18</option>
<option value="19">Prod 19</option>
<option value="20">Prod 20</option>
</select>
click
</div>
</div>
<div data-role="page" id="two">
<div data-role="content">
page 2
</div>
After changing the select menu i need to change the page by clicking the button below the menu. But what happens is, the page change is occured but it will automatically redirect to the previous page where the menu has (In Android). It works fine in desktop. How to prevent this in mobile? and what happens exactly?
Thanks in advance.
NOTE: This is same as this question
but the solution is not working for me...
Even i tried with data-ajax="false" but no luck...

Resources