1

I'm coding a chat box. And the Characters that I enter, is not reflected as it is.

This is basically the code I'm using.

$(document).ready(function() { $(".entry").keydown(function(event) { console.log(String.fromCharCode(event.which)); }); }); 

And so when I type (lower-case) "a", console tab shows me "A".

special characters will not get reflected unless I create separate condition for it.

Could someone help me with a different function which does it all by itself, and returns a string as entered by the user. Or a different approach to this challenge all together. Thanks.

Actual code - chat.js

var str=''; $(document).ready(function() { $(".entry").keydown(function(event) { console.log(event.which); if (event.which === 13 && event.shiftKey === false) { console.log(str); event.preventDefault(); } else { var c = event.which; str = str.concat(String.fromCharCode(c)); } }); }); 

So basically the every character entered would get concated to the string. and Enter key would dump the text to console.

5
  • Do you have any special filters applied for chat box which converts the chars? Commented Mar 7, 2018 at 3:10
  • No i dont think so . var str=''; $(document).ready(function(){ $(".entry").keydown(function(event) { console.log(event.which); if (event.which === 13 && event.shiftKey === false) { //$(".entry").css("background-color", "blue"); //alert($(document.getElementById('entry1').innerHTML)); console.log(str); event.preventDefault(); } else { var c = event.which; str = str.concat(String.fromCharCode(c)); //console.log(String.fromCharCode(c)); } }); }); Commented Mar 7, 2018 at 3:12
  • this is the actual code ... Commented Mar 7, 2018 at 3:13
  • Add this above code in the questio Commented Mar 7, 2018 at 3:13
  • And BTW "entry" is the name of the class of the textarea. Commented Mar 7, 2018 at 3:19

1 Answer 1

2

It's seems that trying to get the value of event.which in keydown event could lead you to a wrong ascii code (What you need to pass to String.fromCharCode).

See https://stackoverflow.com/a/10192144/3879872

I don't know if it fits your needs, but you could try:

$(document).ready(function(){ $(".entry").keypress(function(event) { console.log(String.fromCharCode(event.which)); }); }); 

(Note the use of keypress instead of keydown)

EDIT: Added working Demo

var str = ''; $(document).ready(function() { $(".entry").keypress(function(event) { console.log(event.which); if (event.which === 13 && event.shiftKey === false) { console.log(str); event.preventDefault(); } else { var c = event.which; str = str.concat(String.fromCharCode(event.which)); } console.log('Formated Text', str); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea class="entry"></textarea>

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

1 Comment

Worked like a Charm. Thanks :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.