phil crowe
phil crowe

Reputation: 1505

if an a link contain something in the href change the href to this

i want to check if the href contains 'coming-soon' if it does change the href to go to my products page:

$('a[href$="coming-soon"]').attr('href', '/products.aspx');

cant work this out.

Upvotes: 6

Views: 9801

Answers (4)

Adam
Adam

Reputation: 44939

Use the contains selector.

$(document).ready(function(){
   $("a[href*='coming-soon']").attr('href', '/products.aspx');
});

Try it here!

Upvotes: 1

Matt
Matt

Reputation: 75317

Your selector is currently any anchor tag whose href ends with coming-soon. The contains selector is *=:

$('a[href*="coming-soon"]').attr('href', '/products.aspx');

See http://api.jquery.com/attribute-contains-selector/

If this isn't the problem, ensure the code is ran when the DOM is loaded; by wrapping the code in the $(document).ready()

$(document).ready(function () {
    $('a[href*="coming-soon"]').attr('href', '/products.aspx');
});

Upvotes: 0

simshaun
simshaun

Reputation: 21466

$= means "ends with". You want to find links that contain (anywhere in the URL) coming-soon? Try *= instead of $=.

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630469

$= is "attribute-ends-with", use *= for "attribute contains", like this:

$('a[href*="coming-soon"]').attr('href', '/products.aspx');

Upvotes: 4

Related Questions