Accessing the anchor that was clicked before pjax:send #62

Closed
opened 2016-03-03 06:15:58 -05:00 by howells · 3 comments
howells commented 2016-03-03 06:15:58 -05:00 (Migrated from github.com)

Hi - wonder if anyone could help me, I need to somehow access the anchor that was actually clicked to trigger the pjax:send event. I figured I could do this by listening for the click on any but this event happens after the pjax:send. Any ideas?

Hi - wonder if anyone could help me, I need to somehow access the anchor that was actually clicked to trigger the pjax:send event. I figured I could do this by listening for the click on any <a> but this event happens _after_ the pjax:send. Any ideas?
darylteo commented 2016-03-03 18:31:18 -05:00 (Migrated from github.com)

@howells I trigger pjax call directly.

var pjax = new Pjax({
  'elements': '.some-random-selector-with-no-matches'
});

$('.something').click(function(e) {
  e.preventDefault();
  var target = e.getCurrentTarget; // is what you want

  pjax.loadUrl(url, options); // triggers pjax
});

Note that pjax has to be a singleton, or it may break history (I haven't tested it, but from code inspection it seems that pjax was designed to only be instantiated once). The simplest way to do this is to instantiate pjax with a no-match selector.

@howells I trigger pjax call directly. ``` var pjax = new Pjax({ 'elements': '.some-random-selector-with-no-matches' }); $('.something').click(function(e) { e.preventDefault(); var target = e.getCurrentTarget; // is what you want pjax.loadUrl(url, options); // triggers pjax }); ``` Note that pjax has to be a singleton, or it may break history (I haven't tested it, but from code inspection it seems that pjax was designed to only be instantiated once). The simplest way to do this is to instantiate pjax with a no-match selector.
howells commented 2016-03-04 04:07:21 -05:00 (Migrated from github.com)

Huh that's an interesting approach yeah - thanks. Right now I overrode Pjax.prototype.attachLink with the same function, but prepended it with various stuff that I needed to do, which isn't really sustainable!

Huh that's an interesting approach yeah - thanks. Right now I overrode `Pjax.prototype.attachLink` with the same function, but prepended it with various stuff that I needed to do, which isn't really sustainable!
BehindTheMath commented 2017-12-21 13:09:37 -05:00 (Migrated from github.com)

With #94, you can access the element that triggered the pjax:send event with event.triggerElement.

With #94, you can access the element that triggered the `pjax:send` event with `event.triggerElement`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: iLoveElysia/pjax#62