Im trying to split and replace href using a function. The code is simple and straightforward:
html
<a href="http://nu.nl">nu.nl</a>
Jquery
$(document).ready(function () {
function rep() {
href = $('a').attr('href');
url = href.split('/');
href.replace(url[2], 'cnn.com');
}
rep()
});
As you can see Im calling the function in document ready. I tried ‘prop’ instead of attr with no luck. What am I doing wrong?
Example: JsFiddle
You don’t use what
replacereturns. If you want to change the href in theaelement, you might changeto
Now, supposing you may have more than one
aelement, I’d suggest to replace your whole code withDemonstration
If you prefer, you may also avoid the splitting using a regex :