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

brandonlind2
brandonlind2
7,823 Points

What am I doing wrong with this localStorage?

//page 1

<!DOCTYPE html>
  html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="this is a test">
        <meta name="keywords" content="test">
        <style type="text/css">

        </style>
    </head>
    <body>
        <form>
            <input placeholder="Please enter your name" type="text">
            <button type="submit">click here</button>
        </form>
        <a href="storageTest2.html">click here</a>
    </body>
    <script type="text/javascript">
        let input=document.getElementsByTagName[0];
        localStorage.setItem('name',input.value);


    </script>
</html>

//page 2 == storageTest2.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="this is a test">
        <meta name="keywords" content="test">
        <style type="text/css">

        </style>
    </head>
    <body>
        <h1></h1>
    </body>
    <script type="text/javascript">
        let h1=document.getElementsByTagName[0];
        let h1Str= "Hi" + localStorage.getItem('name') + "thanks for joining";
        h1.innerHTML=h1Str;
    </script>
</html>

fixed code formatting and edited for content

1 Answer

Steven Parker
Steven Parker
231,007 Points

:point_right: I see a few issues:

  • when you call getElementsByTagName, you forgot to provide a tag name argument
  • you probably meant to call it this way: 'getElementsByTagName("input")[0]'
  • your JavaScript executes on page load, before the input can be filled
  • you might want to crete an event handler for the form or for one of the buttons
  • the descriptions could do without the expletives :innocent: