admin管理员组

文章数量:1388161

I want to display the value of an <input> to a label or something like that, by using onkeyup so it's real time. But can only do it by using a second input instead of the label but I just want to display the value.

This is my Javascript function:

function copyTo(obj) {
    document.getElementById("utmatning").value = obj.value
}

And the HTML:

<label>E-post:</label>
<input id="inmatning" type="text" onkeyup="copyTo(this)" placeholder="Skriv in e-post…">
<span>Vår återkoppling kommer att skickas till:</span>
<br>
<label id="utmatning"></label>

I want to display the value of an <input> to a label or something like that, by using onkeyup so it's real time. But can only do it by using a second input instead of the label but I just want to display the value.

This is my Javascript function:

function copyTo(obj) {
    document.getElementById("utmatning").value = obj.value
}

And the HTML:

<label>E-post:</label>
<input id="inmatning" type="text" onkeyup="copyTo(this)" placeholder="Skriv in e-post…">
<span>Vår återkoppling kommer att skickas till:</span>
<br>
<label id="utmatning"></label>
Share Improve this question edited Sep 27, 2013 at 12:32 Jason P 27k3 gold badges33 silver badges45 bronze badges asked Sep 27, 2013 at 12:31 user2362791user2362791 252 gold badges2 silver badges5 bronze badges 1
  • 1 u need to use "innerHTML" if u want content from <label id="utmatning"></label> – user2663434 Commented Sep 27, 2013 at 12:34
Add a ment  | 

4 Answers 4

Reset to default 2

use textContent for label

 document.getElementById("utmatning").textContent = obj.value

DEMO

You can also call the keyup function of jquery and update the label on every keystroke: http://api.jquery./keyup/

try this

$( "#inmatning" ).keyup(function() {
    $("#utmatning").text($("#inmatning").val());
});

Or

$( "#inmatning" ).keyup(function() {
    $("#utmatning").html($("#inmatning").val());
});

both should work

    Try this also if you are seeking short method use jquery :

    $( "#inmatning" ).keyup(function() {
        $("#utmatning").text($(this).val());
    });

    or use 
    $( "#inmatning" ).keyup(function() {
        $("#utmatning").text($("#inmatning").val());
    });

but you are using function then you can also use:

function copyTo(that){
 $("#utmatning").text($(that).val());
}

本文标签: jqueryHTMLjavascriptDisplaying input value to a labelStack Overflow