Hi i am very new to coding. I am trying to create a contenteditable div that is one line only and prevents the user typing more characters when the [contenteditable] reaches the same width as the parent element (e.g. i don't want it to overflow). The below code works on Desktop but not android or IOS. It seems there are 2 issues; 1) the return false; does not exit on android + IOS. 2) android + IOS do not recognise the "e.which" numbers...on android all keyboard letters are 229. My question is how do I get this code to work on mobile platforms please? Many thanks!
Note: On android all keys are e.which = 229 except Delete = 8 and Enter = 13. The code returns for 8 & 13 but not the rest 229.
<div class="textarea">
<div contenteditable class="global-text-field">Lorum Ipsum</div>
</div>
<style>
.global-text-field{position:absolute; white-space: nowrap;}
.textarea {position:relative; width:200px; height:50px; background:yellow;}
</style>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script>
$('.global-text-field').on('keydown', function(e){
return OverflowOneLine(e);
});
function OverflowOneLine(e){
var left = parseFloat($(event.target).css('left'));
var right = parseFloat($(event.target).css('right'));
<!--e.which 8 = Delete, 46 = Backspace, 37 = ArrowLeft, 39 = ArrowRight-->
if(right <= 6 && e.which !=46 && e.which !=37 && e.which !=39 & e.which !=8){
return false;
}
return e.which != 13;
}
</script>