DEV PSC
DEV PSC

Reputation: 11

Javascript - How to replace a certain occurence of a anchor's href contents?

I'm using a widget (Purechat) that allows customers and operators to communicate to each other. I've ran into an issue where anchors' href values inside this widget are being appended with "http://%20", thus making them unclickable to our users. We are investigating the code, however, I would like a quick fix for this by replacing all href contents that contain "http://%20" and replace that portion of the href with an empty string so my anchors work.

What would be the best way to go about this?

Upvotes: 0

Views: 66

Answers (2)

pablito.aven
pablito.aven

Reputation: 1165

You can run a foreach jquery function which runs over every anchor whose href starts with that string, then cut it with substring method and set it's href value again.

This should work:

$("a[href^='http://%20']").each(function(){
  var oldHref = $(this).attr('href');
  var newHref = oldHref.substring(10, oldHref.length);
  $(this).attr('href',newHref);
});

Upvotes: 1

Griffith
Griffith

Reputation: 3217

$('a').attr('href', function(index, value) { 
  return value.replace("//%20", ""); 
});

Upvotes: 1

Related Questions