KiYu KiYu - 2 months ago 11
Javascript Question

How to save / add all special characters like ( " & ' ) ETC

Special Characters

! @ # $ % ^ & * ( ) _ + = - { [ } ] : ; " ' | \ < , > . ? /


I have a simple saving of data. I notice that if I enter all special characters in a textbox and not using a
escape
method all special character after the
&
sign is being cut.

With Out Escpae

Output

! @ # $ % ^


With Escape

Here

JS

var txt = $("txtbox").val();


Notice that the output has a
%20%
how I remove it?.

Answer

%20 is space. If you wish to remove it, you can do it like,

var str = `! @ # $ % ^ & * ( ) _ + = - { [ } ] : ; " ' | \ < , > . ? /`;
var escapes = escape(str.split(' ').join(''));
// or escape(str.replace(/\s+/g, ''));
alert(escapes);

And here's your fiddle

Note that escape is deprecated. Use encodeURIComponent instead.

Comments