I'm using select2 and in my site. I need to know at some point if the dropdown is open or closed. I've studied the documentation but I don't see how this can be done. For example, something like this would be nice:
if ($('select').select2('isOpen') === true) { ... }
Any suggestions?
In version 4.0 of select2 you can listen to select2:opening, select2:open, select2:closing and select2:close events on select element, for example:
$('select').on('select2:open', function (e) {
// select2 is opened, handle event
});
Select2 4.0 has an isOpen method. If #mySelect is your HTML select element then:
$('#mySelect').data('select2').isOpen()
...will return true or false depending on the state of Select2.
By doing some code inspection, it looks like select2-dropdown-open is the class that it adds. But there is an event select2-open in the documentation that fires when the dropdown is open. You can use that to set a variable, or perform an action (also select2-close).
You can do something like this:
$("#e11").on("select2-open", function() {
$(this).data("open", true);
});
$("#e11").on("select2-close", function() {
$(this).data("open", false);
});
if ($("#e11").data("open")) {
//do something
}
2018 Edit
It appears that the names of the events have been updated since 2014. See user1636505's answer below.
As of Select2 4.0.6, this has been updated to the following
$("#foo").select2("isOpen")
This will return true/false
Hope this helps!
change is fired whenever an option is selected or removed.
select2:open is fired whenever the dropdown is opened. select2:opening is fired before this and can be prevented.
select2:close is fired whenever the dropdown is closed. select2:closing is fired before this and can be prevented.
select2:select is fired whenever a result is selected. select2:selecting is fired before this and can be prevented.
select2:unselect is fired whenever a result is unselected. select2:unselecting is fired before this and can be prevented.
It's better to do this:
var select2 = $('#selectorname').data('select2');
if (select2.opened()) {
//do it
} else {
//dont do it
}
$('select').select2('isFocused');
https://github.com/select2/select2/issues/39
It works perfectly.
$(".select2-container").is(":visible")
Related
My HTML page has a Button and a Select Drop-Down (Combo Box). Drop-Down change event is like this:
$('#DDL_ID').on('change', function (e) {
// Some Code Here
});
When I click the button, I am setting some value to Drop-Down and it is working fine.
$('#BTN_ID').on('click', function (e) {
$('#DDL_ID').val('123');
});
When I click the button, Drop-Down value is getting changed but Drop-Down change event is not firing. Can anyone help me on this?
Please note this is not a duplicate question.
Setting the val() through jquery will not automatically trigger the event on the item... so you need to manually trigger() it...
$('#BTN_ID').on('click', function (e) {
$('#DDL_ID').val('123').trigger('change');
});
I am using the ng-multiselect-dropdown package in angular 5 for creating a multi-select dropdown.
I want to do call a function on close or hide of the drop-down component.
like this
closeDropdown : function(){
console.log("dropdown close triggered");
}
According to the documentation you can pass closeDropDownOnSelection value true to close the dropdown whenever the selection is done
ng-multiselect dropdown
Incase of multiple selection you can call (onSelect)="onItemSelect($event)"
for more information check this Demo documentation
You can call the function within (change) event.
ex :
<ng-multiselect-dropdown
(blur)="closeDropdown($event)"
>
</ng-multiselect-dropdown>
To solve the bug identified by satira ( I couldn't comment due to low reputation), ie.
"When the component which has this multi-dropdown opens for the first time or you reload the page and click anywhere outside the dropdown, onDropDownClose() gets called." For me, it didn't happen after the first time. Anyway, i solved it by getting the id of any element on the screen(header, footer or any div) and used docuement.getElementById('element_id').click() on ngAfterViewInit.
ngAfterViewInit() { document.getElementById('header').click(); }
This made sure that no sideeffects take place on my app. I know this is a messy solution but since closeDropdown() of ng-multidropdown doesn't work, this was my only way out.
I had this issue recently and found a solution that works for me using a combination of (ngModelChange) and (click). When using ng-multiselect-dropdown the other normal HTML Element triggers like (blur) and (change) don't work, but the (ngModelChange) does work. Only problem with that is it triggers when being initialized. But I added a boolean variable to the (click) trigger that does seem to work.
Note that this also works to cover the onSelect, onDeSelect, etc
component.ts:
...
dropDownSelect: boolean = false;
dropDownSelection: number;
...
saveFunction(event) {
if(!this.dropDownSelect) return;
...
this.dropDownSelect = false;
}
component.html:
...
<ng-multiselect-dropdown [data]="dataSource" [(ngModel)]="dropDownSelection" [settings]="dropDownSettings" (click)="dropDownSelect = true" (ngModelChange)="saveFunction($event)"></ng-multiselect-dropdown>
...
I tried #misterz's solution but it didn't work. However I modified it and it works perfectly.
The trick:
In addition to (onDropDownClose), listen to a click event;
// this act as a differentiator between other calls(bug) and an intended call
(click)="dropDownSelect = true".
In your component, declare your variable and use it like this:
dropDownSelect = false;
saveFunction($event) {
if (this.dropDownSelect) {
// close the opening to subsequent actions
this.dropDownSelect = false;
// Perform action;
};
}
I am trying to write a code which will make HTML dropdowns readonly but not "disabled" because I want to capture the default values in current form that are coming from previous form.
I have written the below code which is working perfectly fine in Chrome but not working in IE. What could be the possible solution to this.
Below is the jquery code that I have written.
$("#Q4Q25xP1_1, #Q4Q25xP1_2, #Q4Q25xP1_3, #Q4Q25xP1_4, #Q4Q25xP1_5").each(function(){
$(this).on("mousedown", function(e){
return false;
}).on("change", function(){
$(this).find('option').each(function(i, opt) {
opt.selected = opt.defaultSelected;
});
}).css("background-color","grey");
});
Try this one, just disable options which are not selcted
$("#Q4Q25xP1_1,#Q4Q25xP1_2,#Q4Q25xP1_3").find("option").each(function () {
if ($(this).attr("selected") != "selected") {
$(this).attr("disabled", 'disabled');
}
});
and here is the jsfiddle for reference https://jsfiddle.net/3v0w9n3r/
And it works in all browsers including IE.
You can try setting the pointer-events to none using CSS:
<select style="pointer-events:none;">
....
$("#Q4Q25xP1_1, #Q4Q25xP1_2, #Q4Q25xP1_3, #Q4Q25xP1_4, #Q4Q25xP1_5").each(function(){
$(this).attr('disabled','disabled');
}
"Change" event is not consistent in browser,In Firefox and Chrome it work properly
but in IE need to clicked TWICE, first to remove "indeterminate" state, then again to fire the change event. so you need to use trigger click event to click second time so event initialize and work.
So you need to use trigger mousedown for second time apply mousedown event on same element than change event work properly
I was wondering if the following is possible. I would like to fire different events depending upon which option is selected in a dropdown. So far I thought of this but nothing is happening when I select the option
$('#strand_id option[value="5"]:selected').on('click', function(){
console.log('something just happened!!!')
});
I don’t think I can use the change method as this will apply the same event no matter what option is selected (unless there is a way?).
Try this:
$('#strand_id').on('click ', function () {
if (this.value == 5) console.log('something just happened !! !')
});
jsFiddle example
I am using Jquery, for selecting value of drop-down.
I need do do some action on OnChange event (.change in jQuery) , but if there is only one option in drop-down I am not able to capture it in onChange event.
My drop-down is formed dynamically, so don't know how many options I'll get how many options, but I am facing problem if there is just one option.
Is there any way to capture OnChange event for <select> with only one <option>?
You don't need to have an onchange event. Just have a function that gets called from onchange, and if there is only one item, just go ahead and call that function.
function populate() {
//do work to populate #selector
if($("#selector option").length == 1) {
$("#selector").hide();
workerFunction();
} else {
$("#selector").show();
}
$("#selector").change(workerFunction);
//the following line of code will work like a default:
workerFunction();
}
function workerFunction() {
//put whatever used to be in your onChange function here
}
onchange event occurs when the internal state of the element is changed. Since you have a dropdown with a unique value, its state is never changed so the event is not fired.
<label for="checkboxThatUsedToBeASelect">
<input type="checkbox" id="checkboxThatUsedToBeASelect" name="checkboxThatUsedToBeASelect" value="something" />
</label>
$('#checkboxThatUsedToBeASelect').change( function(){
});
Now you have a more user-friendly input, and it'll trigger the change when it is checked or unchecked. :D
How about a conditional event binder? Something like:
var changeHandler = function (method) {
alert(method);
};
if ($('select option').length > 1) {
$('select').change(function() {
changeHandler('change');
});
} else {
$('select').click(function() {
changeHandler('click');
});
}
It sounds like you have a HTML error in your options tags.
In addition to that, in order to target something created dinamically with jQuery, you have to use delegators.
$('selector').change( function(e){...}); // without delegator
$('body').on('change','selector', function(e){...}); // with delegator, it detects even an element added dinamically
This said, you should be abble to detect on change for new and old elements
.change looks for another inmate to switch places. in your case sadly, there is a home alone case and hence the event is not firing.