I have a use case, where i have to select all <a>
, containing string in url like "/web/local" and remove "/web/local" from all href of all these links.
Note: i can't use jQuery. I can use either pure js or YUI.
Thanks in advance.
See comments inline:
let phrase = "/web/local";
// Get all the links that contain the desired phrase into an Array
let links = Array.prototype.slice.call(document.querySelectorAll("a[href*='" + phrase +"']"));
// Loop over results
links.forEach(function(link){
// Remove the phrase from the href
link.href = link.href.replace(phrase, "");
});
// Just for testing:
console.log(document.querySelectorAll("a"));
<a href="http://www.something.com/web/local">Some Link</a>
<a href="http://www.something.com/web/local">Some Link</a>
<a href="http://www.something.com/web/local">Some Link</a>
<a href="http://www.something.com/web/local">Some Link</a>
<a href="http://www.something.com/web/local">Some Link</a>