aftab
aftab

Reputation: 45

how to convert form text input to ASCII in Javascript?

how to convert text input to ASCII and display in text area..

HTML

<div class="form-group">
    <label for="exampleInputPassword1">Favorite Food</label>
    <input type="text" class="form-control" id="fFood" placeholder="Favorite Food" required>
    <textarea name="txt_output"></textarea>
</div>

Upvotes: 0

Views: 3405

Answers (4)

lisandro
lisandro

Reputation: 506

If you want the Ascii codes of the input string in the text area you can try this:

    var txtoutput = '';
var txtinput = document.getElementById('fFood').value;
for(let n=0;n<txtinput.length;n++)
txtoutput = txtoutput + txtinput.charAt(n) + ' -> ' +  txtinput.charCodeAt(n)+'\n';
document.getElementById('textArea').value = txtoutput;

This will show each character with its ascii code. Like: P -> 80 I -> 73 Z -> 90 Z -> 90 A -> 65

Upvotes: 0

alessandrio
alessandrio

Reputation: 4370

jquery

$(function(){
    $('input[type=text]').keyup(function(){
    var x = $('input[type=text]').val();
    $('textarea').val(x);
    });
});

javascript

<script>
function myFunction()
{
var x = document.getElementById("fFood").value;
document.getElementById("ta").value=x;
}
</script>

and html

<div class="form-group">
    <label for="exampleInputPassword1">Favorite Food</label>
    <input type="text" class="form-control" id="fFood" onkeyup="myFunction()" placeholder="Favorite Food">
    <textarea name="txt_output" id="ta"></textarea>
</div>

Upvotes: 0

jhyap
jhyap

Reputation: 3837

I assume what you want mean is to display the "text" typed in textBox in textArea

If so, and here you go: try here by clicking the button to display text in text area

The JS:

function display(){
var result = document.getElementById('fFood');
var txtArea = document.getElementById('textArea');
txtArea.value = result.value;
}

EDIT if you want to get the ASCII code from a string: try it here.

source of reference

Upvotes: 1

jfriend00
jfriend00

Reputation: 707786

If what you mean is how do you get the character code of a character from a javascript string, then you would use the string method str.charCodeAt(index).

var str = "abcd";
var code = str.charCodeAt(0);

This will technically be the unicode value of the character, but for regular ascii characters, that is the same value as the ascii value.

Working demo: http://jsfiddle.net/jfriend00/ZLRZ7/


If what you mean is how you get the text out of a textarea field, you can do that by first getting the DOM object that represents that object and then by getting the text from that object:

var textareas = document.getElementsByName("txt_output");
var txt = textareas[0].value;

If you then want to put that text into the input field, you can do that with this additional line of code:

document.getElementById("fFood").value = txt;

Upvotes: 0

Related Questions