I am looking for event in fabricjs for selection clear - javascript

In my code i have used fabricjs event on selection created now i am looking for selection clear event is it available in canvas
canvas.on('selection:created', function(e) {
activeGroup = canvas.getActiveGroup();
// my code
});
Now I am looking for
canvas.on('selection:clear', function(e)
or similar like that

You can use the 'selection:cleared' event and 'before:selection:cleared' (an event that triggers just before the 'selection:cleared') event, like this:
In your js file add this snippet and you are OK:
function observe(eventName) {
logObservingEvent(eventName);
canvas.on(eventName, function(e){
console.log(eventName)
});
}
observe('before:selection:cleared');
observe('selection:cleared');
I think that you don't need a jsfiddle example, because it is very simple.
Good luck.

canvas.on('selection:cleared',function(e){/**Put ur code here...*/});

canvas.on(selection:cleared`, function(e) {/*** your code **/}

Related

Manually trigger an event on window object

I am adding a listener like so:
window.addEventListener('native.showkeyboard', function (e) {
...
...
});
I'm writing a unit test for this so I want to trigger the event. Im doing:
window.trigger('native.showkeyboard');
But I end up with an error for that line saying:
undefined is not a function
How can I manually trigger this event?
EDIT
I have also tried:
$(window).trigger('native.showkeyboard');
but the handler doesnt run with this as it's not registered with jquery...
If you are triggering the event via jQuery then the event ought to have been attached via jQuery -- see #fredericHamidi's comment.
$(window).on('native.showkeyboard', function (e) {
.........
});
$(window).trigger('native.showkeyboard');
WORKING JSFIDDLE DEMO
Or if you're using plain vanilla JS do it this way:
window.addEventListener('native.showkeyboard', function (e) {
........
});
window.dispatchEvent( new Event('native.showkeyboard') );
WORKING JSFIDDLE DEMO
well you are not working with a jQuery object...That would be your problem.
window.trigger('native.showkeyboard'); //<-- window is not a jQuery object
You need to wrap it
$(window).trigger('native.showkeyboard');

Trigger Bootstrap typeahead on paste event

I've been trying to manually trigger the typeahead search on right-click paste by catching the paste event as follows but I can't seem to find a way to trigger the typeahead's 'matcher' function manually to query the entered string.
$('#search-bar').bind("paste", function(e)
{
$(this).trigger("keydown"); // Tried keyup, input to no avail!
});
Any help would be much appreciated!
Shamelessly copied code from https://stackoverflow.com/a/15179532/559079
$('#search-bar').typeahead({
'updater' : function(item) {
myAjaxFunction(item);
}
});
function myAjaxFunction (item){
$.ajax({ //DO STUFF HERE });
}
$('#search-bar').bind("paste", function(e){
var self = $(this);
setTimeout(function(){self.trigger("keydown");}, 0);
});
(1) my lazy solution: call $('#yourtypeaheadfield').trigger('keyup') to "impersonate" a keyup / keypress event .. this is how Typeahead hooks into the JQuery API.
(2) "better" answer: fire the typeahead event exlicitly - $().trigger('typeahead.updater') - I could not figure out the syntax, though, and it is not documented (at least not for bootstrap 2.3.2).

jQuery on(click) doesn't work but on(hover) does

After initialize js I create new <div> element with close class and on("click") function doesn't work.
$(document).on('click', '.post-close', function () {
alert("hello");
});
but on('hover') work perfectly.
$(document).on('hover', '.post-close', function () {
alert("hello");
});
but I need to make it work on click.
It's because you're not preventing the default behaviour of the browser. Pass e into your handler and then use e.preventDefault()
$(document).on('click', '.post-close', function (e) {
e.preventDefault();
alert("hello");
});
Edit
Also, bind the handler before creating the new <div>
why not use something like
$('.post-close').click(function(){
//do something
});
If the element was added dynamically use:
$(document).on('click', '.post-close', function(){
//do something
});
edit:
like danWellman said, you can add the preventDefault IF you want to make sure no other code is executed. otherwise use the code above.
edit2:
changed the .live to .on
It's an old post but I've had a exactly same problem (element created dynamically, hover works, but click doesn't) and found solution.
I hope this post helps someone.
In my case, I found ui-selectable is used for parent element and that was preventing from click event propagate to the document.
So I added a selector of the button element to ui-selectable's 'cancel' option and problem solved.
If you have a similar probrem, check this
Try turn of libraries for parent element
You're not using stopPropagation() in parent element ?

How to check if there is already a click/event associated to an element

lets say I have
function trigger(){
$('a.pep').each(function(){
$('a.pep').click(function(){
console.log($(this).val());
});
});
}
function push(){
$('body').append('<a class="pep">hey mate i have no trigger yet</a>');
trigger(); //now i do but the others have duplicated trigger
}
$(document).ready(function(){
$('a.push').click(function(){
push();
});
});
So it seems that the click event is being applied twice/+ because the console.log is lauched more than once by click
How can i prevent this?
The problem is that you call $('a.pep').click() lots of times. (In fact, you bind as many click handlers as there are matching elements to each element. And then you do it again every time one of them is clicked.)
You should lever the DOM event bubbling model to handle this. jQuery helps you with the on method:
$(document.body).on('click', 'a.pep', function() {
console.log('element clicked');
$(document.body).append('<a class="pep">Click handlers handled automatically</a>');
});
See a working jsFiddle.
Note that I have removed the val call, because a elements can't have a value... Note also that the on method is introduced in jQuery 1.7; before that, use delegate:
$(document.body).delegate('a.pep', 'click', function() {
Small change to your trigger function is all you need. Just unbind the click event before binding to ensure that it is never added more than once. Also, you don't need to use each when binding events, it will add the event to each item automatically.
function trigger(){
$('a.pep').unbind('click').click(function() {
console.log($(this).val());
});
}
You can check using data('events') on any element if the required event is attached or not. For example to check if click event is attached or not try this.
if(!$('a.pep').data('events').click){
$('a.pep').click(function(){
console.log($(this).val());
});
}
you should use jQuery live here because you add DOM elements dynamicly and you want them to have the same click behaviour
function push(){
$('body').append('<a class="pep">hey mate i have no trigger yet</a>');
}
$(document).ready(function(){
$('a.push').click(function(){
push();
});
$('a.pep').live('click', function(){
console.log($(this).val());
});
});
Try:
if($('a.pep').data('events').click) {
//do something
}
i think if you use live() event you dont need to make function
$('a.pep').live('click', function(){
console.log($(this).val());
});

Add click event to Div and go to first link found

I think I've been too much time looking at this function and just got stuck trying to figure out the nice clean way to do it.
It's a jQuery function that adds a click event to any div that has a click CSS class. When that div.click is clicked it redirects the user to the first link found in it.
function clickabledivs() {
$('.click').each(
function (intIndex) {
$(this).bind("click", function(){
window.location = $( "#"+$(this).attr('id')+" a:first-child" ).attr('href');
});
}
);
}
The code simply works although I'm pretty sure there is a fairly better way to accomplish it, specially the selector I am using: $( "#"+$(this).attr('id')+" a:first-child" ). Everything looks long and slow. Any ideas?
Please let me know if you need more details.
PS: I've found some really nice jQuery benchmarking reference from Project2k.de here:
http://blog.projekt2k.de/2010/01/benchmarking-jquery-1-4/
Depending on how many of these div.click elements you have, you may want to use event delegation to handle these clicks. This means using a single event handler for all divs that have the click class. Then, inside that event handler, your callback acts based on which div.click the event originated from. Like this:
$('#div-click-parent').click(function (event)
{
var $target = $(event.target); // the element that fired the original click event
if ($target.is('div.click'))
{
window.location.href = $target.find('a').attr('href');
}
});
Fewer event handlers means better scaling - more div.click elements won't slow down your event handling.
optimized delegation with jQuery 1.7+
$('#div-click-parent').on('click', 'div.click', function () {
window.location.href = $(this).find('a').attr('href');
});
Instead of binding all the clicks on load, why not bind them on click? Should be much more optimal.
$(document).ready(function() {
$('.click').click(function() {
window.location = $(this).children('a:first').attr('href');
return false;
});
});
I would probably do something like;
$('.click').click(function(e){
window.location.href = $(this).find('a').attr('href');
});

Categories