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 JavaScript and the DOM (Retiring) Making Changes to the DOM Modifying Elements

Emona Arosh
PLUS
Emona Arosh
Courses Plus Student 13,007 Points

I don't know how to solve task 2 of this exercise to add the text content of the Anchor tag to the input value

I don't know how to solve task 2 of this exercise, to Set the text content of the a tag to be the value stored in the variable inputValue.

app.js
const input = document.querySelector('input');
let inputValue = input.value;
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>
Jamie Carter
seal-mask
.a{fill-rule:evenodd;}techdegree
Jamie Carter
Front End Web Development Techdegree Student 12,096 Points

So the aim is to set the value of the input element to the variable inputValue

First we need to locate the HTML input element using javascript.

document.getElementById('linkName')

Then we need to access the value data for the input element.

document.getElementById('linkName').value

We can do this by adding the .value to the end of the element locator.

Finally save this to the variable:

inputValue = document.getElementById('linkName').value
Steven Parker
Steven Parker
231,271 Points

Jamie, that's another way to solve task 1 but the question is about task 2.

2 Answers

Steven Parker
Steven Parker
231,271 Points

Task 2 is almost the opposite of task 1. Instead of putting a value from a form element into a variable, in task 2 you will put the value from that same variable into another element.

So the terms will be on opposite sides of the assignment, and you'll use a different selector and access a different property.

I'll bet you can get it now without an explicit spoiler.

Jamie Carter
seal-mask
.a{fill-rule:evenodd;}techdegree
Jamie Carter
Front End Web Development Techdegree Student 12,096 Points

you need to use similar methods. The key thing here is that you need to target the link.

document.getElementById('link').innerHTML = inputValue