简体   繁体   中英

Javascript: Getting ASCII code of last character in textarea

In my javascript program I am trying to display ASCII code of last character of TextArea. For that I am getting value of textArea, getting last character of text area and using charCodeAt(139) to get the code of last character but it's not working. Can anyone tell me what's wrong in my code.

code:

<!doctype html>
<html>
<head>
    <meta charset=utf-8>
    <title>signature</title>
    <style>
        #key{
            color: #ffffff;
            font-size: 20px;
            text-align: center;
        }

    </style>
</head>
<body bgcolor="#3d382a">

    <h2 style="color: #ffffff; text-align: center;"> MY MESSAGE </h2>
    <form action="#" method="post">

        <p style="text-align: center; color: #ffffff; font-size: 20px">
        <textarea id="textarea" name="text" maxlength="140" rows="15" cols="40" ></textarea><br><br>
        <span id="count"></span> characters</p><br>
        <p id="key">  </p> 

    </form>

        <script>
            var el_t = document.getElementById('textarea');
            var length = el_t.getAttribute("maxlength");
            var el_c = document.getElementById('count');
            el_c.innerHTML = length;
            el_t.onkeyup = function () {
            document.getElementById('count').innerHTML = (length - this.value.length) ;

            var str = document.getElementById('textarea').value;
            var lastChar = str.charAt(str.length - 1); 
            document.getElementById('key').innerHTML = "Last key in ASCII code: " + lastChar;
            };


        </script>
    </body>
    </html>

Change your last line. Right now you just are logging the last character, whilst you need to get the ASCII code.

document.getElementById('key').innerHTML = "Last key in ASCII code: " + lastChar.charCodeAt(0);

 var el_t = document.getElementById('textarea'); var length = el_t.getAttribute("maxlength"); var el_c = document.getElementById('count'); el_c.innerHTML = length; el_t.onkeyup = function () { document.getElementById('count').innerHTML = (length - this.value.length) ; var str = document.getElementById('textarea').value; var lastChar = str.charAt(str.length - 1); document.getElementById('key').innerHTML = "Last key in ASCII code: " + lastChar.charCodeAt(0); }; 
 <h2 style="color: #ffffff; text-align: center;"> MY MESSAGE </h2> <form action="#" method="post"> <p style="text-align: center; color: #ffffff; font-size: 20px"> <textarea id="textarea" name="text" maxlength="140" rows="15" cols="40" ></textarea><br><br> <span id="count"></span> characters</p><br> <p id="key"> </p> </form> 

Move your last three lines into your onkeyup function, this will update the #key element every time the input is updated. You also want to use str.length - 1 in the charCodeAt function, as you want to get the code for the last character, not just the 140th.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM