I have a need to capture an event (like a click) on a object that is elsewhere on the page and not a child of the select2 object while the select2 object has focus and is showing results. When the select2 results object is focused, I cannot click on a button/anchor elsewhere on the page and have it perform its action. It merely closes the select2 object.
Is there a way to do this? Here is a demo that shows this behavior: http://codepen.io/anon/pen/JoJobW
<!-- Javascript snippet -->
$(document).ready(function() {
$("#e1").select2();
$("#e2").click(function(){
alert("click!");
e.preventDefault();
});
});
<!-- HTML snippet -->
<select id="e1"></select>
Click here when select2 is focused
Using select2 v3.5.2 and jQuery 1.11.0
Select2 places a "mask" element behind the drop-down that captures the mouse click. Others have complained about this.
Here is a jsfiddle that shows the mask element.
You could try to remove the mask element when the drop-down is opened.
$('#e1').select2().on('select2-open', function() {
$('.select2-drop-mask').remove();
});
Of course, this means the drop-down will not close when the user clicks off of it, but you could add an event handler on the document (or body) to do that.
$(document).click(function() {
$("#e1").select2('close');
});
You have to make sure the click event on the other element does not propagate to the document in that case.
$('#e2').click(function() {
alert('click!');
return false; // Prevent default action and stop propagation.
});
jsfiddle
Related
I'm following the modal dialog example on jQuery UI website but not having any luck getting the dialog to be displayed. The example creates and opens the dialog from a button-cllick but I'm trying to launch it from a click on a TD element. Is that possible?
Assume that your td element has clickable_td id and also your dialog block has dialog id, So you can attach click event handler for td emelent with the on() function object and show your dialog block with dialog() function object while clicks captured like this:
$('#clickable_td').on('click',function(){$('#dialog').dialog();});
NOTE: The default jQuery z-index is 100, if your layouts' elements have higher value of it, try this instead:
$('#clickable_td').on('click',function(){$('#dialog').dialog();$('div.ui-front').removeClass('ui-front').css({zIndex: "10000"})});
This code basically creates a block UI. Instead of using jquery block UI dialog, you can use jquery blockUI plugin. You can refer the link to understand the code.
http://www.malsup.com/jquery/block/#demos
In the below example #yourtd_id is the id of your td and #your_div is the div which you want to make as modal. When the td is clicked .blockUI is a class which display your_div on the screen. Now it like a modal dialog. To come out of the modal dialog insert a button on #your_div and give id as #cancel_btn. When the #cancel_btn is clicked .unblockUI() unblocks the UI i.e unblocks the modal dialog.
$(document).ready(function() {
$('#yourtd_id').click(function() {
$.blockUI({ message: $('#your_div') });
//keep a cancel button on #your_div. for eg: consider its id as #cancel_btn
});
$('#cancel_btn').click(function(){
$.unblockUI();
});
});
This will work.I hope this will help you.
I have a page with an accordion that contains search criteria. Once a person clicks the search button, the criteria closes (I simulate a click on the accordion heading). This works great.
The problem is when there the user provides invalid input, my AJAX call returns and I attempt to show the search criteria again before the accordion finished closing (again with a simulated click on the accordion heading). This of course fails to open the search criteria.
A simple example of this would be double clicking on the accordion heading. Nothing happens if the accordion is in transition during this double click.
How to avoid this in my situation? NOTE: It seems to be more of a problem in Chrome/FF than in IE as IE does not animate the accordion open/collapse.
Basic idea from the code where #section1 represents the accordion heading:
$("#section1").click();
//some very fast AJAX.
$("#section1").click();
JS Fiddle: http://jsfiddle.net/mikeyfreake/gGaJn/2/
Thanks,
Mike
Call stop on the children of the accordion:
$("#accordion").children().stop(true, true);
http://jsfiddle.net/gGaJn/9/
Use async: false parameter in your Ajax call. This will let the Ajax call finish executing before the second click.
http://api.jquery.com/jQuery.ajax/
Or you can perform the second click in the success function:
$.ajax({
success: function (data) {
$("#section1").click();
},
...
});
I am using jQuery UI Autocomplete plugin on a textarea
What I want is the textarea content should not be modified when the user hovers on the autocomplete list
This means, if the user scrolls though the list or hovers on the options, the textarea contents should not change. The change should occur only on selection of the option.
$("#my_textarea").autocomplete({
source: mySource,
focus: function (event, ui) {
event.preventDefault() // <-- prevent the textarea from being updated.
}
});
Example: http://jsfiddle.net/hJ9sA/
I am working on drag and drop tool using jQuery UI's sortable widget.
I'd like to add a message into an empty div where something can be dragged into, like: "drag here". I'd like to remove this message as soon as something is in that div. There will be times when the page loads with something already in that div, so it can't be only on action, but onload needs to check it too.
How do I go about it?
Here's my code:
$("#divFrom, #divTo").sortable({
connectWith: '.connectedSortable'
}).disableSelection();
You should be able to set up a draggable, and droppable and tap into droppable's drop event handler, which is fired when an item is dropped:
$("#target").droppable({
drop: function() {
// Empty the droppable div:
$(".message").remove();
}
});
Demo here: http://jsfiddle.net/andrewwhitaker/rUgJF/2/
As for doing something similar on load, if you provided your markup it would make providing a solution a little easier (is there a specific element inside the droppable div that you could check for?)
Hope that helps.
I have an image that opens the jQuery UI Slider div on click and allows users to set a value by dragging the handle. What I'm trying to do now is hide that handle when the user clicks anywhere else on the page, but the regular .blur event doesn't seem to work.
$("#openPriceToSliderGif").click(function(){
$("#slider-vertical").show();
$("#slider-vertical").focus();
});
$("#slider-vertical").blur(function () {
$("#slider-vertical").hide();
});
Probably you will have a better luck on defining global onclick handler and there you need to check if source of event is not your slider. If not - do your magic.
Basically - if you spinner doesn't include element such as text field or link it will not support focus/blur
Ok, this is what I have put together to make this work.
Thanx DroidIn.net for your help.
$(document).bind("click", function(e){
if(e.target.id != "openPriceToSliderGif")
$("#slider-vertical").hide();
return false;
});
$("#openPriceToSliderGif").click(function(){
$("#slider-vertical").toggle();
});