[ACCEPTED]-jQuery / Javascript replace <space> in anchor link with %20-replace

Accepted answer
Score: 21

You'd be better off using the native javascript 1 encodeURI function.

$(".row a").each(function(){
  $(this).attr( 'href', encodeURI( $(this).attr("href") ) );
});
Score: 19

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");
});
Score: 3

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"));
});
Score: 1

@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

Score: 0

You can replace "" like so:

$(document).ready(function () {
    $("#content a").each(function (){
        $(this).attr('href', $(this).attr("href").replace("%20",""));
    });
});

0

Score: 0

I know this is super late, but I found that 1 the unescape() method tends to work too...

Score: 0

in ES2021 use replaceAll()

 $(".row a").each( function() {
        this.href = this.href.replaceAll('',"%20");
    });

0

More Related questions