Callback for d3 click event - d3.js

I've got an event listener for click:
circle.on('click', function() { ... });
Can I use a callback to do something after the click event handling has occurred?

I'm not entirely sure what you mean, but you can run arbitrary code inside the click handler, including code that is run after the "normal" event handling is done. You can also trigger other events from that code to allow you to use other callbacks.

Related

An alternative to 'selectionChange' event?

I want to execute a function after the user hilites some text.
I was hoping to listen for the 'selectionChange' event for that purpose, but the documentation states that it doesn't fire for every case, plus I've noticed that it actually also fires just by clicking into the editable area. Is there another event the my plugin can listen for?
This worked for me.
CKEDITOR.instances.editor1.document.$.addEventListener('selectionchange', function (e) { //selection change logic goes here });

Running event function after event has been set up

This will do it:
$('input').on('change', function(event){
...
}).change();
...but what if there is another plugin installed that hooks some function on the change event? I'll trigger that function too, and it may not be desirable. How can I avoid such conflicts?
Use namespaced events
$('input').on('change.myevent', function(event){
...
}).trigger('change.myevent');
This will get triggered on normal change events (along with other change handlers on it) but will also be triggered by change.myevent (only it)
This will also allow you to unbind only your own event in case you need to ..

Prevent jquery mobile panel from opening?

I have the following code. As You can see I have tried most of the common methods for preventing it from opening.
I'm doing this because in the panelbeforeopen I would like to check to see if the user is logged in and then only show the panel if they are.
$(document).on('panelbeforeopen', '#right_panel', function(event){
event.preventDefault();
alert('test');
return false;
event.stopImmediatePropagation();
});
In the end I had to bind an event handler to the button that was responsible for opening the panel.
One note is that the code that will link an a tag automatically to a panel sets up its event handler as a click event not a tap event. It also for what ever reason will not work if you use .on to bind your click event handler. You must bind it like normal.
See js fiddle here
$('.footer .right_panel').on('click', function (event) {
alert('test');
event.stopPropagation();
});

Bind custom event handler after ajax load

Specifically I'm looking to bind lightbox to a specific element. Normally I would just do this: $('a.lightbox').lightBox(); but that isn't working since I'm doing some loading with AJAX. Looking at the jQuery API I found .bind() and .live() but I'm not getting anything when I do $('a.lightbox').bind('lightBox') after the AJAX .load() call.
What am I missing?
You need to add a callback function that handles that.
$("#div").load(url, {}, function(){ $('a.lightbox').lightBox(); });
Bind isn't going to help you, as the event isn't getting an event fired on it.
Another way would be to bind to an element higher up in the dom and check the target type. Such as:
$('#div').bind('click', function (event) {
target = $(event.target);
if (target.hasClass('lightbox')) {
// do stuff here
}
});
Just don't go too far up or you'll be catching way too many clicks.

jQuery: Targeting elements added via *non-jQuery* AJAX before any Javascript events fire? Beyond the scope of live()?

Working on a Wicket application that adds markup to the DOM after onLoad via Wicket's built-in AJAX for an auto-complete widget. We have an IE6 glitch that means I need to reposition the markup coming in, and I am trying to avoid tampering with the Wicket javascript... blah blah blah... here's what I'm trying to do:
New markup arrives in the DOM (I
don't have access to a callback)
Somehow I know this, so I fire my
code.
I tried this, hoping the new tags would trigger onLoad events:
$("selectorForNewMarkup").live("onLoad", function(){ //using jQuery 1.4.1
//my code
});
...but have become educated that onLoad only fires on the initial page load. Is there another event fired when elements are added to the DOM? Or another way to sense changes to the DOM?
Everything I've bumped into on similar issues with new markup additions, they have access to the callback function on .load() or similar, or they have a real javascript event to work with and live() works perfectly.
Is this a pipe dream?
.live() doesn't work like this, it's a common misconception. .live() creates an event handler at the DOM root and waits for events to bubble up to it. If the selector matches the event target, .live() will fire the bound event.
It doesn't look for new objects and bind events to them in any way, rather it just listens for a bubble, and doesn't care when that object was added to the DOM.
You need to fire whatever code is needed to run manually when your load operation completes.
What will this is the livequery plug-in, look specifically at the livequery( matchedFn ) call.
You can do something like this:
$('#myID').livequery(function() { $(this).offset()...stuff });
i guess this is what you are looking for http://ananthakumaran.github.com/2010/02/19/wicket-post-ajax-handling.html

Resources