Reputation: 36078
I have a text below where I want to swap out the login link, but only if the text says "Log In". Sometimes is will say "My Account":
<div id="Login_T4304F19B025">
<a href="/login1">Log in</a>
</div>
How can I use jQuery to change the href to /login2 if the link text is "Log in"?
Upvotes: 1
Views: 255
Reputation: 344517
You can use :contains()
:
$("a:contains('Log In')").prop("href", "/login2");
However, :contains()
works like a wildcard match (e.g. it would also match "Please Log In Now"). If you want a more restrictive search, use .filter()
:
$("a").filter(function () {
return $.text(this) == "Log In";
}).prop("href", "/login2");
Upvotes: 3
Reputation: 34855
var a = $('#Login_T4304F19B025 a').text();
if(a == "Log in" || a == "My account"){
a.attr("//new link");
}
Upvotes: 0
Reputation: 973
if ($('#Login_T4304F19B025 a').text() == 'Log in'){
$('#Login_T4304F19B025 a').attr('href','/login2');
}else{
$('#Login_T4304F19B025 a').attr('href','/login1');
}
Upvotes: 2
Reputation: 76870
You could do:
$('a:contains("Log in")').attr('href', '/login2')
Upvotes: 0
Reputation: 501
$("div a").attr("href","some link to somewhere");
hope this works
Upvotes: 1