0

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?

0

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

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

or better:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.