I'm trying to accomplish a live character counter for a text input field, but cannot seem to make it work.
(我正在尝试为文本输入字段完成实时字符计数器,但似乎无法使其正常工作。)
The onkeypress-function either go as undefined or is just called once when loading the page(onkeypress函数要么未定义,要么在加载页面时被调用一次)
Simply assigning the function with onkeypress=" " does not seem to work.
(仅使用onkeypress =“”分配功能似乎无效。)
Additionally, I want to update the text of charcountLabel;
(另外,我想更新charcountLabel的文本。)
which I cannot seem to do.(我似乎无法做到。)
Simply using ' document.getElementById ' for updating its innerHTML does not work.(简单地使用' document.getElementById '来更新其innerHTML无效。)
- How do I correctly assign keypress-functions to html-elements in .ejs?
(如何正确地将keypress-functions分配给.ejs中的html-elements?)
- How do I access and update innerHTML of other elements?
(如何访问和更新其他元素的innerHTML?)
See code below:
(参见下面的代码:)
<input type="text" id="textContent" onkeypress="charcount">
// Should be live-updated with the length of input text above.
<span id="charcountLabel"> 0 </span>
<script>
function charcount() {
var characterCount = document.getElementById("textContent").innerText.length;
document.getElementById("charcountLabel").innerHTML = characterCount;
}
</script>
ask by B. Ginner translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…