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

Aziz Kemal HOSCAN
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Aziz Kemal HOSCAN
Full Stack JavaScript Techdegree Graduate 16,113 Points

Need help on solving jquery

It seems fine to me. I am getting element by its ID and use Jquery val(); Can not see the problem. Please help

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() {
  const newName = document.getElementById('name-input').val();
});

2 Answers

Dimitar Dimitrov
Dimitar Dimitrov
11,800 Points

You are using mixed code to select the element. 1st. Your are using JavaScript to select the element -document.getElementById('name-input') 2nd. You are using Jquery method to get the value -val()

The challenge ask you to use only Jquery to select the element and get the value. in that case your code should look like this : ** var newName = $('#name-input').val();**

$('#name-input') is Jquery way to select things in the DOM, and document.getElementById('name-input') is one of the methods you can use in JavaScript to select DOM elements

So remember : $('#name-input') - Jquery document.getElementById('name-input') - JavaScript

And as i said the challenge ask you to use Jquery methos to complete it