6

I want to sent some html special character to text field and then later use javascript to get it back in its original format: like if I sent "&pi", it will shows "π" on the text input, and when I use javascript to get it back, I should get "&pi", but I only can get "π", not "&pi". Please help, my code is like the following:

<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" />

3 Answers 3

3

Depending on what you need the it for you could do one of 3 options:

  1. use the javascript escape and unescape functions:

    var escaped = escape('π') // escaped = "%u03C0" which unescape will turn back into π

  2. If you have jQuery available it's html method might do the trick:

    $('input').val().html()

  3. This is a hack but will work: Save a copy of the value the way you want it returned on a 'data-' attribute of the input element, this will validate in html5 and won't break in other doctypes.

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the help, I use escaped form for data, and unescaped form for display, it works okay.
Cool, I'm glad to be of help :)
1

The value property will give you text, not HTML.

You could try getAttribute('value') but since &pi; and π are identically equivalent in HTML, a browser is entirely free to return π for that too.

Once the HTML has been converted into a DOM, there is no way to access the original HTML (short of downloading the HTML again with XHR and parsing the raw text of it yourself).

If you want the data to be "&pi;" then the HTML for it needs to be &amp;pi;.

If you want that and the rendering on the button to be π then you need:

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

(Keeping in mind the various bugs that old-IE has with the button element)

That will, of course, display &pi; when you copy the value to the text input.

2 Comments

Thank you for your help, but document.getElementById('input').getAttribute('value') return null.
Oh, missed that this was a different element you were getting the value from. getAttribute returns the attribute value (which is the default value of the input) not the current value.
-1

Use this to replace all occurrences of the character:

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

3 Comments

nice workaround, but I need to make it for all special chars, not just π.
@KelvinLi You could maintain an array of special entity mappings running the replace over.
I tried, but it looks like replace() cannot use for very special chars like π.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.