JavaScript JavaScript and the DOM Making Changes to the DOM Modifying Elements

Brian Miller
PRO
Brian Miller
Pro Student 6,696 Points

Select the <input> element with the ID linkName and store its value in the variable inputValue.

Not sure what is going on, I have selected the ID of the input element: document.getElementById("linkName");and placed its value: linkName, in the variable const inputValue = document.getElementById("linkName"); I have run the code outside of workspaces, and get no errors. I have previewed the code in workspace and everything looks good. My understanding is to use the document.getElementByID method to store the value of the input element (linkName) in a variable named inputValue. What am I missing?

html> <head> <title>DOM Manipulation</title> </head> <body> <div id="content"> <label>Link Name:</label> <input type="text" id="linkName"> <a id="link" href="https://teamtreehouse.com"></a> </div> <script> const inputValue = document.getElementById("linkName"); </script> </body> </html>

app.js
const inputValue = document.getElementById("linkName");
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>DOM Manipulation</title>
    </head>
    <link rel="stylesheet" href="style.css" />
    <body>
        <div id="content">
            <label>Link Name:</label>
            <input type="text" id="linkName">
            <a id="link" href="https://teamtreehouse.com"></a>
        </div>
        <script src="app.js"></script>
    </body>
</html>
Brian Miller
Brian Miller
Pro Student 6,696 Points

I should also add that I was able to successfully complete this code after a google search using :

let inputValue = document.querySelector('#linkName').value;

Why would the getElementByID not work? Thats what the code challenge task called for, right?

let inputValue = document.getElementByID('#linkName').value;

3 Answers

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 47,481 Points

This works:

var inputValue = document.getElementById('linkName').value

The difference between that and the following you posted:

let inputValue = document.getElementByID('#linkName').value;

The d in Id is lowercase

No #

Shelley Brown
Shelley Brown
7,613 Points

This actually worked. Thanks