Javascript Form How to - Get char code for data in textarea








Question

We would like to know how to get char code for data in textarea.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-2.0.2.js'></script>
<style type='text/css'>
.no-wrap {<!--   w ww .jav  a 2 s .  co m-->
  resize: none;
  white-space: nowrap;
  overflow: auto;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    $("#do").click(function(){
        var value = $("#input-1").val();
        $("#char-1").text(value.charCodeAt(value.length - 1));
        value = $("#input-2").val();
        $("#char-2").text(value.charCodeAt(value.length - 1));
    });
});
</script>
</head>
<body>
  <textarea id="input-1" placeholder="Type a space here"></textarea>
  <br /> Char code:
  <span id="char-1"></span>
  <br />
  <textarea id="input-2" class="no-wrap" placeholder="Type a space here"></textarea>
  <br /> Char code:
  <span id="char-2"></span>
  <br />
  <button id="do">Get Chars</button>
</body>
</html>

The code above is rendered as follows: