<textarea id="tx"></textarea>
<p>Line: <span id="num">1</span></p>
<script>
const tx = document.getElementById('tx');
const num = document.getElementById('num');
const update = () => {
// カーソル位置までの文字列を切り出し、改行(\n)の数を数える
num.innerText = tx.value.substr(0, tx.selectionStart).split('\n').length;
};
// 入力時・キー操作時・クリック時に更新を実行
tx.oninput = tx.onkeyup = tx.onclick = update;
</script>
使用変数
| id | |
| innerText | |
| num | |
| onclick | |
| oninput | |
| onkeyup | |
| tx | |
| update |