How to use javascript to get html special characters in text input

I want to send some special html character to a text field and then use javascript to return it in its original format: for example, if I sent "& pi" it would display "π" on the text input and when I use javascript to return it I should get "& pi", but I can get "π" and not "& pi". Please help, my code is as follows:

<script type="text/javascript"> 
function qpush(a) {
    document.getElementById('input').value += a;
}

function show(a) {
    alert(document.getElementById('input').value);
}
</script>

<input type="text" id="input" />
<input type="button" onclick="qpush('&pi;');" value="&pi;" />
<input type="button" onclick="show()" value="go" />
+5
source share
3 answers

Depending on what you need, you can do one of three options for you:

  • use javascript escape and unescape functions:

    var escaped = escape ('π')//escaped = "% u03C0", unescape π

  • jQuery, html :

    $( ''). (). HTML()

  • , . , , "data-" , html5 DOCTYPEs.

    document.getElementById('input'). setAttribute ('data-originalValue', '& pi')

+3

value , HTML.

getAttribute('value'), &pi; π HTML, π .

HTML DOM, HTML ( HTML XHR ).

, "&pi;", HTML &amp;pi;.

, π, :

<button type="button" value="&amp;pi;">π</button>

( , IE )

, , &pi; .

+1

Use this to replace all occurrences of a character:

document.getElementById('input').value.replace(/π/g,"&pi;");
0
source

All Articles