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

HETAL PATEL
HETAL PATEL
3,161 Points

'undefined' is not a function...

Here they ask to get the code of input field and display it in h1 tag. I fetch the value of input field using val() and display that in setInterval() function. I am not getting on which thing it shows the undefined function error. I tried using text() instead of val() but same thing. Any one can help on this...

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

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

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

function updatePreview(){  
  //Get the user's input
  var titleValue=$titleInput;
  //Set the user input as the preview title. 
  $previewTitle.val(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

Bob McCarty
PLUS
Bob McCarty
Courses Plus Student 16,618 Points

Hetal,

Try placing the value assignment in updatePreview()

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

Bob

HETAL PATEL
HETAL PATEL
3,161 Points

Bob Yes, that also works.

But we declare the variable on the first line to store its value. $titleInput = $("#title").val(); So instead using $("#title").val() again in function, I used Nicholas suggestion, to call the variable $titleInput with val() in function. var titleValue = $titleInput.val();

Well both the things are same either u call $("#title").val() or $titleInput.val(), but some where I read that if u store value in variable, and using the values from variable is fast for traversing aspect. Thanks for replying.

Nicholas Grenwalt
Nicholas Grenwalt
46,626 Points

The only thing that needs modifying for this challenge is the titleValue variable that doesn't have an assignment. Since you want to collect the value of the user's input use the given $titleInput variable and attach the value method to it to be stored in the titleValue variable. Hope that helps clarify some things. Keep up the coding!

var titleValue = $titleInput.val();
HETAL PATEL
HETAL PATEL
3,161 Points

Thanks Nicholas, That works perfectly. I thought .val() can not be used with variables - val() is only for html elements. But good to know that.