user1015214
user1015214

Reputation: 3081

Using javascript replace() inside jquery attr()

I want to use jquery to select a specific anchor tag on my page, and apply the replace() method to it (I am trying to replace &#039 with %27, I am having apostrophe issues when submitting my form...) and I'm not sure exactly how to do this. I started to write this:

    $(".view-subscription-admin tbody td.views-field-nothing a").attr("href

and then realized that I wasn't sure how to use this with the replace function. How would I do this?

Upvotes: 2

Views: 360

Answers (2)

user1015214
user1015214

Reputation: 3081

I'm not sure why the suggested answer didn't work for me, but someone else gave me a different suggestion and this one worked:

$(document).ready(function() {
  $(".view-subscription-admin tbody td.views-field-nothing a").each(function(i) {
            var oldHref= $(this).attr('href');
            var newHref = oldHref.replace(''', '%27');
            $(this).attr('href',newHref);
  });
});

Upvotes: 0

thecodeparadox
thecodeparadox

Reputation: 87073

$(".view-subscription-admin tbody td.views-field-nothing a")
              .attr("href", function(i, oldHref) {
                   return oldHref.replace('&#039', '%27');
               });

.attr() method support a callback function and within that argument you can do your replace code and return that href.

Upvotes: 4

Related Questions