Search code examples
javascriptclasshref

Change href based on class


how to add a prefix before a link only to a certain category of class.

<p class="syndicated-attribution">
    <a href="http://www.example.com/">Read more →</a>
</p>

using js like that

$('.syndicated-attribution a').attr('href', function() {
    return 'http://site.com/' + $(this).text()
});

Eg: http://www.example.com --> http://site.com/http://www.example.com

is very simple, as I do?


Solution

  • $(this).text() Is the content within the tag:

    <a href="site.com">THIS IS THE TEXT</a>
    

    You don't want the text. You want the href. You're already using the attr('href') function, so...