Anyway To Change Href Of Link With No Id And No Jquery?
Answer :
window.onload=function() { var links = document.links; // or document.getElementsByTagName("a"); for (var i=0, n=links.length;i<n;i++) { if (links[i].className==="checkout_link" && links[i].title==="Checkout") { links[i].href="someotherurl.html"; break; // remove this line if there are more than one checkout link } } }
Update to include more ways to get at the link(s)
document.querySelector("a.checkout_link"); // if no more than one document.querySelectorAll("a.checkout_link"); // if more than one
to be even more selective:
document.querySelector("a[title='Checkout'].checkout_link");
Lastly newer browsers have a classList
if (links[i].classList.contains("checkout_link") ...
window.onload = function() { alert(document.querySelector("a[title='Checkout 2'].checkout_link").href); }
<a href="x.html" class="checkout_link" title="Checkout 1" />Checkout 1</a> <a href="y.html" class="checkout_link" title="Checkout 2" />Checkout 2</a>
Comments
Post a Comment