-2

As I got the idea If I want change the anchor tag href by matching an anchor with a specific existing href, I wrote below code that is working:

<a href="Accept.php">Accept</a>
<script src="http://code.jquery.com/jquery-3.2.1.js"></script>
<script>
$("a[href='Accept.php']").attr('href', 'http://www.accept.com');
</script>

But Now I was working on to change the hyperlink by using the anchor tag value using jQuery.

I wrote below code for that but it's not working for me.

$("a[text='Accept']").attr('href', 'http://www.accept.com');
mxr7350
  • 1,438
  • 3
  • 21
  • 29
Doe Chalie
  • 15
  • 2

6 Answers6

1

$("a").filter(function(){

return $(this).text() == 'Accept';
}).attr('href', 'http://www.accept.com');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="Accept.php">Accept</a>
  1. use filter and return the anchor you want to select.
  2. then use attr() to set the attr value
guradio
  • 15,524
  • 4
  • 36
  • 57
0

Please check this. It is working.

$(document).ready(function(){
   $("a:contains('Accept')").attr('href', 'http://www.accept.com');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<a href="Accept.php">Accept</a>
Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33
0

If your a elements don't have a text attribute (and they shouldn't, it's invalid HTML), a[text='...'] isn't going to work.

If you're trying to set their href to their contents, you could use :contains, but note that it matches anywhere in the element:

$("a:contains('Accept')").attr("href", function() {
    return $(this).text();
});

Note that that uses the version of attr accepting a callback, so it uses the specific text of each a as the attribute.

Your content would need to be a valid relative or absolute URL, e.g. <a>Accept.php</a>. or similar.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

you can use contains selector to get link object. refer https://api.jquery.com/contains-selector/

$("a:contains('Accept')).attr('href', 'http://www.accept.com');
-1

As far as I know, jQuery does not allow to select elements by the contents of their inner text. You will need to either filter the results or use a different approach altogether.

First, let's see the filtering option. You will need to select all anchor tags and filter them by their contents afterwards using the filter() function:

$("a")
    .filter(function(idx, el) {
        return $(el).text() === "Accept";
    })
    .attr("href", "http://www.accept.com");

Same example on jsfiddle.

However, this is obviously a rather slow option and on very large pages, it might turn out to be causing a perceivable lag. Make sure it doesn't cause trouble for you before pushing this to production.

An alternative approach, and the recommended one, would be to use a class or id. As you know, jQuery, as well as vanilla JS, is able to select elements by their id or class. I would suggest trying to figure out if you can apply an id/class to your link and select that instead. Use the filtering approach as a last resort solution.

mingos
  • 23,778
  • 12
  • 70
  • 107
-2

Here is sample demo code for you:

HTML:

<a href="Accept.php">Accept</a>

JQuery:

if($("a").text() == 'Accept') {
    $("a").attr('href', 'http://www.accept.com');
}
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35