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

Laura Pesce
seal-mask
.a{fill-rule:evenodd;}techdegree
Laura Pesce
Full Stack JavaScript Techdegree Student 7,875 Points

Help for completing challenge (jQuery)

The challenge is the following:

Take a look at the app.js and preview. The aim of this application is to show what the user types in to the input field and preview it in the H1 tag. Almost all the code is there, there's just one thing missing. Retrieving the value from the input.

//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 titleValue; //Set the user input as the preview title. $previewTitle.text(titleValue); }

Could you help me please?

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
  var titleValue;
  //Set the user input as the preview title. 
  $previewTitle.text(titleValue);
}
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

Steven Parker
Steven Parker
229,744 Points

:point_right: The jQuery .val() method is good for obtaining the contents of an input field.

And the variable $titleInput already exists to conveniently reference the input field.

Jacob Hopkins
Jacob Hopkins
7,310 Points

I'm still lost on this, can someone explain further?