admin管理员组文章数量:1296485
I want text in the span
field to change as the input
field changes live
.
So I have a input
field
<input type="text">
and a span
field
<span></span>
I want text in the span
field to change as I type in the input
field.
I know there's already a question like this that was asked Change span when text field changes. But what makes this question different is the changes should happen as you type i.e. live
.
I want text in the span
field to change as the input
field changes live
.
So I have a input
field
<input type="text">
and a span
field
<span></span>
I want text in the span
field to change as I type in the input
field.
I know there's already a question like this that was asked Change span when text field changes. But what makes this question different is the changes should happen as you type i.e. live
.
3 Answers
Reset to default 6Here is a vanilla JavaScript version.
var input = document.querySelector("[type=text]"),
output = document.querySelector(".output");
function keydownHandler() {
output.innerHTML = this.value;
}
input.addEventListener("input", keydownHandler);
<input type="text">
<span class="output"></span>
Very simple, use a keyup
event.
$(function () {
$("input").keyup(function () {
$("span").text(this.value);
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />
<p><span></span></p>
In the latest browsers, you can use oninput
event.
You can use HTML5 input
event, listen the event using on()
$('.input').on('input', function() {
$('.span').text(this.value)
})
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" class="input">
<br><span class="span"></span>
JavaScript solution with input
event
function change(val) {
document.querySelector('.span').innerHTML = val;
}
<input type="text" oninput="change(this.value)">
<br><span class="span"></span>
本文标签: javascriptChange span when text field changes as you typeStack Overflow
版权声明:本文标题:javascript - Change span when text field changes as you type - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741630747a2389349.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论