0

I'm calling the permalink of post on submitting a form like below:

       var $this = $(this),
            category = $this.attr('data-event-category'),
            action = $this.attr('data-event-action'),
            label = $this.attr('data-event-label')
             permalink =  call_url["permalink"]


        self.track(category, action, permalink);

                console.log('tracking pushed: '+category+', '+action+', '+permalink+','+label)

    });
},

Which I'm calling via my functions.php

    wp_register_script( 'analytics', get_template_directory_uri() . '/js/analytics.js','','1.0.1');
wp_enqueue_script( 'analytics' );
wp_localize_script(
    "analytics"
, "call_url"
, [
        "permalink" => get_permalink($post),
]

);

All works great and appear in my console fine.

Only thing is, I would like it to the permalink to appear as an data-attribute like the category, action and label.

For that I tried the below:

                 permalink =  $this.attr('call_url["permalink"]')

Which doesn't seems t work.

Any help will be super ??

Thank you !

1 Answer 1

0

To set an attribute you will need to do

 $(this).attr('data-url', call_url["permalink"]);

To update

$(this).data('url', call_url["permalink"]);
3
  • Hi Jim, Thanks, May I know what is the 'url' referencing to ? Thanks Commented Oct 30, 2017 at 11:08
  • in the console it bring only the value [object Object] instead of the URl with this . .. Commented Oct 30, 2017 at 11:13
  • I think you need to filter down through the [object,Object] var obj = call_url["permalink"] console.log(obj[0].['permalink']) Commented Oct 30, 2017 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.