Modifying previous_posts link to use AJAX

What am I doing wrong here?

First, and foremost…

I am editing the wp-includes/link-template.php file in WordPress

Don’t hack Core files! Especially when there is a filter that should do what you need.

add_filter(
  'previous_posts_link_attributes',
  function ($clauses) {
    echo 'previous_posts_link_attributes';
    return 'test="attribute"';
  },
  1
);

You could insert your onclick with the filter.

However, your Javascript should be able to hijack the link without any manipulation of the markup at all (without that hackish onclick), which would be the proper way to do this.