RichardU
RichardU

Reputation: 149

jQuery to change href that includes shortcode

When I click on a link that includes a shortcode, I want to change the href by removing any spaces. Here's the link

<div class='strip_space'>
   <a href="https://example.com/[wpbb site:user_meta key='contrib-name']">Testing</a>
</div>

I've gotten this far

jQuery(document).ready(function() {
      jQuery(".strip_space").html(function () {
      this.href = this.href.replace(/\s/g, '');
      return jQuery(this).href().
            });
        });

What am I missing?

Cheers, Richard

Here's the snippet I'm using for enqueueing

function my_scripts_method() {
    wp_enqueue_script(
        'strip-space', // name your script so that you can attach other scripts and de-register, etc.
        get_stylesheet_directory_uri() . '/js/strip-space.js',
        array('jquery') // this array lists the scripts upon which your script depends
    );
  }
add_action('wp_enqueue_scripts', 'my_scripts_method');

Upvotes: 0

Views: 44

Answers (1)

charlietfl
charlietfl

Reputation: 171690

You can use attr(attributeName, function) and target the <a> instead of the <div>

jQuery(function($) {
  $(".strip_space a").attr('href', function(_, curr) {
    return curr.replace(/\s/g, '');
  });
  // display as text for demo purpose
  $(".strip_space a").text(function(){ return this.href})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='strip_space'>
  <a href="https://example.com/[wpbb site:user_meta key='contrib-name']">Testing</a>
</div>

Upvotes: 1

Related Questions