bibi
bibi

Reputation: 3

Insert value of input as string in label

I would like to display the value of the input #nom as a string in the label tag

<input type="text" id="nom">
<input type="button" id="ok" value="afficher" onclick="affiche();">
<label id="msg"></label>

I have tried the following script, but it does not work and returns no errors.

function affiche() {
    var nom = document.getElementById("nom").value;
    $("#msg").val(nom);
}

What am I doing wrong?

Upvotes: 0

Views: 555

Answers (2)

tutankhamun
tutankhamun

Reputation: 920

Try this:

$('#msg').text(nom);

or better:

$('#msg').text($('#nom').val());

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68410

You have a jQuery and plain Javascript salad, but assuming you actually have a reference to jQuery your problem is that label elements don't have value so val method does nothing. Method val applies only to input controls.

Use text() or html() to set label content

$("#msg").html(nom);

Upvotes: 1

Related Questions