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 (2014) Creating a Mobile Drop Down Menu Getting a value

rdaniels
rdaniels
27,258 Points

Code Challenge, JQuery: Getting a value

I keep getting an error: Bummer when I type in "Hello World" I get back "Hello World" in the H1. That is the response expected I assume, but it gives an error. Any help would be appreciated. Thanks....

//Select the input for the title for blog post.
var $titleInput = $("#title");

//Select the preview h1 tag
var $previewTitle = $("#titlePreview");

// Every second update the preview
var previewTimer = setInterval(updatePreview, 1000);

function updatePreview(){  
  //Get the user's input
  var blogTitle = prompt("What is your blog title?");
  var $input = $("#titleInput");
  $("input").val(blogTitle);

  //Set the user input as the preview title. 
  $previewTitle.text(blogTitle);
}
js/app.js
//Select the input for the title for blog post.
var $titleInput = $("#title");

//Select the preview h1 tag
var $previewTitle = $("#titlePreview");

// Every second update the preview
var previewTimer = setInterval(updatePreview, 1000);

function updatePreview(){  
  //Get the user's input  
                                                     // This is the code that I entered
  var blogTitle = prompt("What is your blog title?");
  var $input = $("#titleInput");
  $("input").val(blogTitle);

  //Set the user input as the preview title. 
  $previewTitle.text(blogTitle);
}
index.html
<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" type="text/css" href="theme.css">
</head>
<body>
  <div id="form">
    <label for="title">Blog Post Title</label><input id="title" name="title" value="" placeholder="Enter your blog title here">
  </div>
  <div id="preview">
    <h1 id="titlePreview"></h1>
  </div>
  <script src="//code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
  <script src="js/app.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
theme.css
  #form {
    width: 45%;
    float: left;
  }
  #preview {
    width: 45%;
    float: right;
  }
  label {
    width: 20%;
    display: inline-block;
  }
  input {
    width: 70%;
  }

2 Answers

Mark VonGyer
Mark VonGyer
21,239 Points

var blogTitle = prompt("What is your blog title?"); var $input = $("#titleInput"); $("input").val(blogTitle);

The error is in line 3, if you want to call the jQuery variable you would use $(input).val(blogTitle).

You would be better off cutting out the second line though, and doing:

var blogTitle = prompt("What is your blog title?"); $("#titleInput").val(blogTitle);

rdaniels
rdaniels
27,258 Points

I appreciate your quick response, but I'm still getting the same error using that format. Any other suggestions? I've tried several other ways also and still get the same response from the editor.... ugg...

Mark VonGyer
Mark VonGyer
21,239 Points

Hi Rodney, I just checked your code again. In addition to previous error, should the ID of the element be #titlePreview rather than #titleInput?

I see the H1 tag has the id of preview not input.

Ryan Hunt
Ryan Hunt
16,091 Points

Hi Rodney,

I just finished this too and had a tough time figuring it out. The prompt is handled in the html so you don't have to worry about that in the .js

This is what worked for me.

function updatePreview(){  
//Get the user's input
   var titleValue = $titleInput.val();
//Set the user input as the preview title. 
   $previewTitle.text(titleValue);
}

Since the $titleInput variable is already set at the top I figured that whatever what typed in the box was set to the value of the input field on the markup.