jQuery Mobile, Select All Items in an option list - javascript

Ok, so I have an issue selecting all items in a multiple select in jQuery Mobile.
Here's the fiddle: http://jsfiddle.net/u41yk3fy/
HTML:
<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="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>
</select>
<div class="floatright" data-role="controlgroup" data-type="horizontal">
Select All
Deselect All
</div>
</div>
</div>
Javascript:
function selectAll(select) {
if (select == false) {
$("#sel option:selected").removeAttr("selected");
} else {
$("#sel option").attr("selected", "true");
}
$("#sel").selectmenu("refresh", true);
}
$(document).ready(function () {
$('#selectall').click(function (event) {
return selectAll(true);
});
$('#deselectall').click(function (event) {
return selectAll(false);
});
});
Basically on Chrome, Opera and Safari this actually works. Using IE and Firefox if you select all and deselect all, attempting to select all again no longer works. I have a feeling that this might be a jQuery, jQuery Mobile or Javascript issue and not something I'm doing wrong. However, if I am doing something wrong I'd appreciate the input.

You can try this:
function selectAll(select) {
if (select == false) {
$("#sel").val([]);
} else {
$("#sel option").prop("selected", true);
}
$("#sel").selectmenu("refresh", true);
}
Here is a Demo.

Related

Bootstrap-multiselect show hide div elements

I am using bootstrap-multiselect for my task to show hide dynamic div's element based on multiple selected checkboxes. The StackOverflow question I found here is only exposed about how to show hide multiselect based on selected multiselect, not div.
HTML code :
<select id="one" multiple="multiple">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select id="two" multiple="multiple">
<option data-group="1" value="OneA" disabled>One A</option>
<option data-group="1" value="OneB" disabled>One B</option>
<option data-group="2" value="TwoA" disabled>Two A</option>
<option data-group="2" value="TwoB" disabled>Two B</option>
<option data-group="3" value="ThreeA" disabled>Three A</option>
</select>
JavaScript code:
$(document).ready(function() {
$('#one').multiselect({
onChange: function(element, checked) {
var opts = $('*[data-group="' + element.val() + '"]');
console.log(opts);
if (checked === true) {
opts.prop('disabled', false).prop('selected', false);
} else if (checked === false) {
opts.prop('disabled', true).prop('selected', false);
}
$("#two").multiselect('refresh');
}
});
$('#two').multiselect();
});
Besides, I am a beginner on bootstrap and jQuery. Could someone help me how to solve this issue please. Thank you.
This can be easily achieved.
For instance, add following Divs along your html code indicated above:
<div data-group="1">data-group="1"</div>
<div data-group="2">data-group="2"</div>
<div data-group="2">data-group="2"</div>
<div data-group="3">data-group="3"</div>
<div data-group="3">data-group="3"</div>
and can easily manage Hide/Show of Divs in your js using:
opts.css({'display':'none'}); // to hide
opts.css({'display':'block'}); // to show
<script>
$(document).ready(function() {
$('#one').multiselect({
onChange: function(element, checked) {
var opts = $('*[data-group="' + element.val() + '"]');
console.log(opts);
if (checked === true) {
opts.prop('disabled', false).prop('selected', false);
opts.css({'display':'block'});
} else if (checked === false) {
opts.prop('disabled', true).prop('selected', false);
opts.css({'display':'none'});
}
$("#two").multiselect('refresh');
}
});
$('#two').multiselect();
});
</script>
You may customize it as you wish. e.g., you may need to change data-group to myDiv-group for div elements and modify the javascript accordingly.
Hope this helped :)

show hide div based on select option value jQuery

I have a select list with value 0,1,2,3,4,5,6,7 .On select with value 0 and 1 I want to show a div, but on select with value 2,3,4,5,6,7 I want to hide that div.The code I have right now is below
HTML
<select class="rel_status">
<option value="0">---</option>
<option value="1">Single</option>
<option value="2">In a relationship</option>
<option value="3">Engaged</option>
<option value="4">Married</option>
<option value="5">Separated</option>
<option value="6">Divorced</option>
<option value="7">Widowed</option>
</select>
<div class="rel_part">
<input type="text" name="part_name" placeholder="Search">
</div>
jQuery
//hide partner search form
$('.rel_part').hide();
//Search Relationship partner
$(document).ready(function(){
$('.rel_status').change(function(){
if($('.rel_status').val() == '2','3','4','5','6','7') {
$('.rel_part').show();
} else {
$('.rel_part').hide();
}
});
});
The code actually works when I put
.val() == '2')
instead of
.val() == '2','3','4','5','6','7')
But then I cant show the div for other values
You can do it like this if ($.inArray($(this).val(), "2,3,4,5,6,7") == -1) {
Demo
//hide partner search form
$('.rel_part').hide();
//Search Relationship partner
$(document).ready(function() {
$('.rel_status').change(function() {
if ($.inArray($(this).val(), "2,3,4,5,6,7") == -1) {
$('.rel_part').show();
} else {
$('.rel_part').hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="rel_status">
<option value="0">---</option>
<option value="1">Single</option>
<option value="2">In a relationship</option>
<option value="3">Engaged</option>
<option value="4">Married</option>
<option value="5">Separated</option>
<option value="6">Divorced</option>
<option value="7">Widowed</option>
</select>
<div class="rel_part">
<input type="text" name="part_name" placeholder="Search">
</div>
use this it will works
$('.rel_status').change(function(){
if($('.rel_status').val() < 2) {
$('.rel_part').show();
} else {
$('.rel_part').hide();
}
});
//hide partner search form
$('.rel_part').hide();
//Search Relationship partner
$(document).ready(function(){
$('.rel_status').change(function(){
if($('.rel_status').val() < 2) {
$('.rel_part').show();
} else {
$('.rel_part').hide();
}
});
});
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<select class="rel_status">
<option value="0">---</option>
<option value="1">Single</option>
<option value="2">In a relationship</option>
<option value="3">Engaged</option>
<option value="4">Married</option>
<option value="5">Separated</option>
<option value="6">Divorced</option>
<option value="7">Widowed</option>
</select>
<div class="rel_part">
<input type="text" name="part_name" placeholder="Search">
</div>
That's not a valid way of comparing one value to many others.
if($('.rel_status').val() == '2','3','4','5','6','7')
Use a logic OR Operator instead:
if($('.rel_status').val() == '0' || $('.rel_status').val() = '1') {
$('.rel_part').hide();
} else {
$('.rel_part').show();
}
Notice, that I switched the cases in the if clause so it's less code to write.
The problem is this is not a valid way to use , in Javascript, because you will end up validating if($('.rel_status').val() == '7') { instead.
You can change the code into this
if($.inArray($('.rel_status').val(), ['2','3','4','5','6','7']) != -1) {
and it should work.
Hope it helps!

jquery programmatically reselecting multselect does not reselect

I am trying to select all options in a multselect with a tickbox using jquery 2.1.1.
This works fine on the first tickbox select and deselect, however subsequent selects of the tickbox do not highlight the multiselect options and the change event writing to console displays null for the values.
Below is a working copy that can be cut and paste to a file to illustrate the problem - naturally I've searched stack overflow for this issue but can't find it listed.
Any idea why a reselecting the tickbox adds 'selected = "selected" but does not highlight the options and results in 'null' for the values in the console?
I appreciate any help.
jQuery(document).ready(function($) {
jQuery(function() {
jQuery('#bundle-option-5').change(function() {
console.log(jQuery(this).val());
});
});
});
function updateBundleItems(bundleOptionId) {
if (jQuery('input#bundle_tick_' + bundleOptionId).is(':checked')) {
//jQuery('.bundle_item_'+bundleOptionId).addAttr("selected");
jQuery('.bundle_item_' + bundleOptionId).attr('selected', true);
} else {
// jQuery('.bundle_item_'+bundleOptionId).removeAttr("selected");
jQuery('.bundle_item_' + bundleOptionId).attr('selected', false);
}
jQuery('#bundle-option-' + bundleOptionId).trigger('change');
}
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<div class="input-box">
<input class="checkbox" onclick="updateBundleItems(5)" id="bundle_tick_5" type="checkbox">
<select multiple="multiple" size="5" id="bundle-option-5">
<option value="">None</option>
<option class="bundle_item_5" value="11">first one</option>
<option class="bundle_item_5" value="12">second one</option>
<option class="bundle_item_5" value="13">third one</option>
</select>
</div>
You should use the prop function instead of the attr one.
Here is the change in your code:
jQuery(document).ready(function($) {
jQuery(function() {
jQuery('#bundle-option-5').change(function() {
console.log(jQuery(this).val());
});
});
});
function updateBundleItems(bundleOptionId) {
if (jQuery('input#bundle_tick_'+bundleOptionId).is(':checked')) {
//jQuery('.bundle_item_'+bundleOptionId).addAttr("selected");
jQuery('.bundle_item_'+bundleOptionId).prop('selected', true);
}
else {
// jQuery('.bundle_item_'+bundleOptionId).removeAttr("selected");
jQuery('.bundle_item_'+bundleOptionId).prop('selected', false);
}
jQuery('#bundle-option-'+bundleOptionId).trigger('change');
}
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<div class="input-box">
<input class="checkbox" onclick="updateBundleItems(5)" id="bundle_tick_5" type="checkbox">
<select multiple="multiple" size="5" id="bundle-option-5">
<option value="">None</option>
<option class="bundle_item_5" value="11">first one</option>
<option class="bundle_item_5" value="12">second one</option>
<option class="bundle_item_5" value="13">third one</option>
</select>
</div>

Select and display content from external div

i tried to display external content div on select option. Here is example
<select>
<option value="http://www.indiansuperleague.com/atletico-de-kolkata/squad/midfielder-3986-arata-izumi-playerprofile">ARATA
IZUMI</option>
<option value="http://www.indiansuperleague.com/atletico-de-kolkata/squad/midfielder-10724-borja-fernandez-playerprofile">BORJA
FERNANDEZ</option>
<option value="http://www.indiansuperleague.com/atletico-de-kolkata/squad/midfielder-10249-clifford-rayes-miranda-playerprofile">CLIFFORD
RAYES
MIRANDA</option>
</select>
<div id="content-display"></div>
When select I want to display content inside <div class="player-detailswrap">. And first option as default.
Please Help
You need change event for select, on which you get the html from selected option value url using jquery get method and then set returned html to required div:
var content_display = $('#content-display');
$('select').change(function(){
$.get(this.value, function( pagehtml ) {
content_display.html( pagehtml );
});
});
Hello there you can try this
$(select).on('change', function(){
$(#content-display).text($(this).val());
})
On the select change event:
Get the selected option value.
Create an iframe with this value as src attribute.
And append it to your div.
This is your way to go:
function displayOption(select) {
var frame = document.createElement("iframe");
frame.src = select.value;
document.getElementById("content-display").innerHTML = "";
document.getElementById("content-display").appendChild(frame);
}
<select onchange="displayOption(this)">
<option value="http://example.com/1">Content 1</option>
<option value="http://example.com/2">Content 2</option>
<option value="http://example.com/3">content 3</option>
<option value="http://example.com/4">Content 4</option>
</select>
<div id="content-display"></div>
This is a jquery solution:
//This code will initialize the div content
//You can use an id with the select if you have many dropdowns in the page
var dropdown = document.getElementsByTagName("select")[0];
$('#content-display').load(dropdown.options[0].value);
function displayOption(select) {
$('#content-display').load(select.value);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select onchange="displayOption(this)">
<option value="http://example.com/1">Content 1</option>
<option value="http://example.com/2">Content 2</option>
<option value="http://www.google.com">content 3</option>
<option value="http://example.com/4">Content 4</option>
</select>
<div id="content-display"></div>
EDIT:
To answer your question about loading a div content from the other page:
If you need to load an external div content, you can use:
function displayOption(select) {
$('#content-display').load(select.value+" #other-page-div-id");
}
(If you're comfortable with using jquery)
$("#select_id").on("input", function(e){
$("#content-display").text($(this).val())
});
Here's a fiddle of it working. on works best for listening for events.
If you would want to do this you can use jQuery AJAX GET or load request and simply feed the selector of the content you want form the page. The only thing is you HAVE to be on the same domain because of Cross Domain Policy.
You can take a look at jQuery Load in docs
$(document).load('test.html #container', fucn...) // Returns #container
Here's the solution
$('#content').change(function() {
$('#content-display').html($(this).val())
});
Assuming your want to load content from url.Use Ajax, it's easily can load contents without page reload.
var toggleContents = document.querySelectorAll('.toggleContent');
if (toggleContents) {
for (var i = 0; i < toggleContents.length; i++) {
toggleContents[i].addEventListener('change', changeContent, false);
}
}
function changeContent() {
var content = document.querySelector(this.dataset.content);
if (content) {
var xhttp = new XMLHttpRequest();
var url_id = '#main-content';
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
content.innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", this.value + url_id, true);
xhttp.send();
}
}
<select class='toggleContent' data-content='#content-display'>
<option value="https://developer.mozilla.org/en-US/docs/Web/JavaScript">Content 1</option>
<option value="https://developer.mozilla.org/en-US/Learn/Getting_started_with_the_web/JavaScript_basics">Content 2</option>
<option value="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays">content 3</option>
<option value="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode">Content 4</option>
</select>
<div id="content-display"></div>
I hope your problem will resolve, Please use the below code. Please vote for me if my answer right for your question.
$(document).ready(function () {
$('#ddlOption').change(function () {
var val=$("#ddlOption").val();
$("#content-display").html('');
$("#content-display").append(val)
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<select id="ddlOption">
<option value="http://example.com/1">Content 1</option>
<option value="http://example.com/2">Content 2</option>
<option value="http://example.com/3">content 3</option>
<option value="http://example.com/4">Content 4</option>
</select>
<div id="content-display"></div>
You can use jQuery in order to do this stuff more easy:
So, the content maybe can't be loaded because of Access-Control-Allow-Origin, this happens when you trying to access a URL you don't own or is reestricted.
Check the code here
jQuery(document).ready(function(){
jQuery("#select-content").change(function(){
var content_url = jQuery("#select-content option:selected").val();
if(content_url){
jQuery("#content-display").load(content_url);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="select-content">
<option value="">-- select content --</option>
<option value="http://example.com/1">Content 1</option>
<option value="http://example.com/2">Content 2</option>
<option value="http://example.com/3">content 3</option>
<option value="http://example.com/4">Content 4</option>
</select>
<div id="content-display"></div>
The best way to achieve this would be some simple jQuery functions. In this example, there are 4 divs with different content in each. All are hidden by default.
Then you can use a bit of jQuery to hide and show certain divs based on the selection you make:
$(document).ready(function(){
$('.hidden').hide();
$('#dropdown').on('change', function() {
if (this.value == 'defaultoption') {
$('.hidden').hide();
}
else if (this.value == 'select1') {
$('.hidden').hide();
$("#content1").show();
}
else if (this.value == 'select2') {
$('.hidden').hide();
$("#content2").show();
}
else if (this.value == 'select3') {
$('.hidden').hide();
$("#content3").show();
}
else if (this.value == 'select4') {
$('.hidden').hide();
$("#content4").show();
}
else
{
$("#content-display").hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="dropdown">
<option value="defaultoption">Select Content</option>
<option value="select1">Content 1</option>
<option value="select2">Content 2</option>
<option value="select3">Content 3</option>
<option value="select4">Content 4</option>
</select>
<div id="content-display">
<div id="content1" class="hidden">This is some text for content 1</div>
<div id="content2" class="hidden">This is some text for content 2</div>
<div id="content3" class="hidden">This is some text for content 3</div>
<div id="content4" class="hidden">This is some text for content 4</div>
</div>
Hope this helps!

div to show select menu value with jquery

i have a question regarding Jquery
<select id="thechoices">
<option value="0">Select Box</option>
<option value="box1">Box 1</option>
<option value="box2">Box 2</option>
<option value="box3">Box 3</option>
</select>
<!-- the DIV -->
<div id="value"> disabled till you make a choice</div>
while the select menu have the choice Select box i need the div to be shown as a small black screen with the disabled message
once the user change the select menu and choose another option the value div shown the selected option for example Box 1 or Box 2
$(document).ready(function() {
// event triggered when options changed
$('#thechoices').change(function() {
// check if first option or other is selected
if ($(this).val() == 0) {
$('#value').addClass('disabled');
$('#value').html($('#value').data('default'));
} else {
$('#value').removeClass('disabled');
$('#value').html($(this).val());
}
});
});
Check out the fiddle: http://jsfiddle.net/gwKfP/1/
Here is a working Example: http://jsfiddle.net/sHqFX/
<select id="thechoices">
<option value="0">Select Box</option>
<option value="box1">Box 1</option>
<option value="box2">Box 2</option>
<option value="box3">Box 3</option>
</select>
<!-- the DIV -->
<div id="value"></div>
Put this between head tags
<script type="text/javascript">
$(document).ready(function(){
$("#thechoices").change(function(){
if($(this).val() == 0)
{
$("#value").html("disabled till you make a choice");
$("#value").css("background-color","black");
$("#value").css("color","white");
}
else{
$("#value").html($("#thechoices option:selected").text());
$("#value").css("background-color","white");
$("#value").css("color","black");
}
});
});
</script>
Here's a quick solution, just add this code:
CSS:
<style>
.disabled{
background-color: gray;
}
</style>
JavaScript
function onSelectedOption(){
var selectedText = $("#thechoices option:selected").text();
if($("#thechoices").val() == "0")
$('#value').addClass("disabled").text(selectedText );
else
$('#value').removeClass("disabled").text("");
}
$(document).ready(function(){
onSelectedOption(); //With this we 'disable' when page loads
$('#thechoices').change(onSelectedOption);
});
Hope this helps
Use the jQuery 'change' event. http://api.jquery.com/change/
$('#thechoices').change(function(){
$('#value').css('display','block');
});
$('#thechoices').change(
function() {
$('#value').text(
$('#thechoices :selected').text()
)
}
);

Categories