Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript jQuery Basics Introducing jQuery Getting Values from Form Fields

Laura Rodriguez
Laura Rodriguez
8,284 Points

Confuse again

Following the instruction it should work in this way: $('button').click(function() { // Write code here $('#name-input').val('newName') }); I got check in www.w3schools.com, and they provide a closer example, which is totally similar, but it is not working $("button").click(function(){ $("input:text").val("Glenn Quagmire"); }); });

index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body>
    <h2 class="profile-name">Treasure Porth</h2>
    <p class="profile-text">I am a web developer!</p>

    <label>Change name:<input id="name-input" type="text"></label>
    <button>Change</button>

    <script
    src="jquery-3.2.1.min.js"></script>
    <script src="app.js"></script>
</body>
</html>
app.js
$('button').click(function() {
  // Write code here
    $('#name-input').val('newName')
});

4 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Laura Rodriguez,

You are almost there but you appear to have misread what the first task is asking for, let's review the task.

  • Use jQuery’s val() method to get the value of the text input with the ID of name-input
  • Save it to a variable named newName

So what you are been asked to do is retrieve the value of the field name-input and store it in a variable called newName. Going back to basics, JavaScript variables begin most commonly with the var keyword followed by the name and the assigned value.

Instead, you are overwriting the value of name-input with newName which is the opposite of what the challenge expects. Looking at the .val() method in jQuery, we can retrieve the value of an input field simply by calling this method on our name-input field.

Hope that helps.

Laura Rodriguez
Laura Rodriguez
8,284 Points

Hi Chris,

I still there ...I tried the following

$('button').click(function(newName) { // Write code here $('#name-input').val(newName) }); When I create the variable separated it is not working : var:newName;

Laura Rodriguez
Laura Rodriguez
8,284 Points

I got the solution!

$('button').click(function() { // Write code here const newName = $('#name-input').val(); $('#name-input').html("#name-input"); });

Thank you!

Laura Rodriguez
Laura Rodriguez
8,284 Points

and the second part:

$('button').click(function() { // Write code here const newName = $('#name-input').val(); $('#name-input').html("#name-input"); $(".profile-name").text(newName);

});