Change 'href' attribute in AJAX enabled links

by Marius Ilie   Last Updated January 12, 2018 11:07 AM

I have some links with the class use-ajax. Everything works fine with default settings.

The problem is that at some point I need to change the href attribute of a link and make the ajax call to the new url, which is not working. The call is made to the initial url of that link.

I tried to reattach behaviours but this doesn't seem to work.

Any help would be appreciated.

Any ideas? Thanks

Tags : 7 ajax

Answers 3

Since nobody knows how to do it I think this might help:

var element_settings = {};
element_settings.url = $(this).attr('href');
element_settings.event = 'click';
element_settings.progress = {
  type: 'throbber',
  message: ''
var base = $(this).attr('id');
Drupal.ajax[base] = new Drupal.ajax( base, this, element_settings );


$(this).bind(Drupal.ajax[base].event, function (event) {
  return Drupal.ajax[base].eventResponse(this, event);
Marius Ilie
Marius Ilie
July 17, 2012 09:33 AM

Drupal link based Ajax, correspond to a Drupal.ajax['#id'] element object and drupal works with them not with DOM Objects.

So you don't have to change the href attribute you need to change the URL where it's doing POST in the Drupal.ajax element, in this case do this:

// Confirm structure first executing on console 'Drupal.ajax['#id'].options.url'
var href = 'hook_url/ajax/parameter1/parameter2';

// And change the href POST parameter
Drupal.ajax['#id'].options.url = href;

And it's done!

December 10, 2012 17:22 PM

in case your link has no ID attribute, here is another solution:

var href = oldhref.replace('/watch/', '/unwatch/'); //your new url
var elem = $('.watchthis a'); // the link element 
elem.attr('href', href);
var n = Drupal.ajax.instances.length;
for (var i = 0; i < n; i++) {
  if (Drupal.ajax.instances[i].element == elem[0]) {
    Drupal.ajax.instances[i].options.url = href+'?_wrapper_format=drupal_ajax';
January 12, 2018 10:58 AM

Related Questions

Decoding cyrillic %get[] parameter in Ajax form

Updated June 11, 2015 08:03 AM

How to reload Google chart on ajax submit?

Updated August 12, 2015 17:03 PM

grab and reuse AJAX request parameters in a view

Updated July 17, 2015 13:03 PM