[ACCEPTED]-jQuery / Javascript replace <space> in anchor link with %20-replace
Accepted answer
You'd be better off using the native javascript 1 encodeURI
function.
$(".row a").each(function(){
$(this).attr( 'href', encodeURI( $(this).attr("href") ) );
});
Your approach is correct, but you're forgetting 2 to set the new value once you replace it. Try 1 this:
$(".row a").each( function() {
this.href = this.href.replace(/\s/g,"%20");
});
You have to set the attribute value ( attr(key, value)
), in 1 your code you are only reading its value:
$(".row a").each(function(){
$(this).attr('href', $(this).attr("href").replace(/\s/g,"%20"));
});
@Naresh Yes there is a way for that, see 3 the below example:
Decode a URI after encoding 2 it:
<script type="text/javascript">
var uri="my test.asp?name=ståle&car=saab";
document.write(encodeURI(uri)+ "<br />");
document.write(decodeURI(uri));
</script>
The output of the code above will be:
my%20test.asp?name=st%C3%A5le&car=saab
my test.asp?name=ståle&car=saab
for 1 more details visit here
You can replace ""
like so:
$(document).ready(function () {
$("#content a").each(function (){
$(this).attr('href', $(this).attr("href").replace("%20",""));
});
});
0
I know this is super late, but I found that 1 the unescape()
method tends to work too...
in ES2021 use replaceAll()
$(".row a").each( function() {
this.href = this.href.replaceAll('',"%20");
});
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.