I know the title wasn't very good but I have a div
that has contenteditable="true"
basically when CTRL + B is pressed the text you type should go bold and it works. But when I toggle it on it sometimes switches the bold on and off here is a snippet for it:
<html>
<head>
<style>
.textbox {
border: 1px solid;
border-radius: 5px;
width: 50%;
height: 170px;
outnline: none;
}
.textbox: focus {
border: 2px solid;
}
</style>
</head>
<body>
<div onkeyup="boldText()" contenteditable="true" class="textbox">
</div>
<script>
function boldText() {
if (event.keyCode == 17 + 66) {
document.execCommand('bold');
}
}
</script>
</body>
</html>
Well, first, document.execCommand();
is deprecated. I would try this.
function boldText(e) {
if (e.keyCode == 17 && e.keyCode == 66) {
let textBox = document.querySelector('.textbox');
textBox.style.fontWeight = 'bold';
}
}
document.addEventListener('keyup', boldText);
.textbox {
border: 1px solid;
border-radius: 5px;
width: 50%;
height: 170px;
outline: none;
}
.textbox: focus {
border: 2px solid;
}
<html>
<body>
<div contenteditable="true" class="textbox">
</div>
</body>
</html>