1/14 Did you create a variable called $h1?


#1

This is my code:

$h1 = $("<h1>Hello</h1>");

Why I cannot pass?
And it remind me of "Did you create a variable called $h1?"


#2

I tested your code, there seems to be nothing wrong with it:

$h1 = $("<h1>Hello</h1>");

Maybe it has something to do in the index.html section, when I removed the

<script type='text/javascript' src='script.js'></script>

from the

<head></head> tags

It returned the error: Oops, try again. Did you create a variable called $h1?


Maybe you just need to add

<script type='text/javascript' src='script.js'></script>

in the header tags and then it should work fine

--Hope this answered your question :smile_cat: and keep coding :sunglasses:--


#3

Here is my css code:

<!DOCTYPE html>
<html>
    <head>
        <title>Result</title>
        <script type='text/javascript' src='script.js'></script>
    </head>
        <body></body>
</html>

And here is my js code

$h1 = $("<h1>Hello</h1>");

And I still cannot pass and got: Oops, try again. Did you create a variable called $h1?
Thanks anyway.


#4

@2333luffy Don't give up, there might still be a solution to your problem my friend :grin:

  • You said " Here is my css code " , if the stylesheet.css part is enabled you shouldn't put the code there, put it in the index.html part. If this is not the case try the below options.

  • What browser are you using? If you're using IE maybe try using another browser such as chrome or mozilla firefox

  • If you are using chrome or firefox, do you have added extensions or plugins that might prevent index.html from working? To test if this is true try do an exercise from a topic that involves an html part. If there is an interference, try to disable these extensions or plugins.

    • do you have a semi-open comment on top of your code?

      <!-- or /*
    • do you have an incorrect spelling in the open $(document).ready(); function? If this is the case here's the full correct code:

$(document).ready(function(){ //the code goes in here });
  • Are you writing code or some other text not shown in your above presentation of your work, out of the

<html></html> tags or document.ready(); function?
  • if none of these work, Maybe try creating another account?

#5

Hello!

You forgot To add "var" before "$h1":

var $h1 = $("

Hello

");

#6

I don't think so.The example don't add "var" either.


#7

Did you try my code before posting?


#8

Hi @flaxh @2333luffy

 $h1 = $("<h1>Hello</h1>");


and

var $h1 = $("<h1>Hello</h1>");

are equivalent

You can test both codes in the lessons index.html and script.js tab:
https://www.codecademy.com/courses/web-beginner-en-v6phg/0/1?curriculum_id=50a3fad8c7a770b5fd0007a1

and the result will still be the same and would allow you to pass through to the next lesson.


(Below this line is some of the info from the lesson)


Creating HTML Elements


Dynamically adding elements to our HTML page is a powerful tool—it lets us modify not only the formatting, but the actual structure of our websites in response to a user's actions. For example, when you get a Gchat, each message is actually a new

being dynamically added to the page. Cool, right?

If you think about it, we've sort of done this already: all we're doing is setting a variable equal to a jQuery object. In this case, however, instead of just having something like:

$p = $('p');

We'll want to pass in an entire HTML element in quotes:

$p = $("<p>I'm a new paragraph!</p>");

When we put text in quotes like this, we call it a string (as in a "string of characters"). From now on, when we say "string," you can think "text" or "phrase." Strings are always in single or double quotes.




although the lesson doesn't directly state that you should use var in front of your code it would still be acceptable


Final solution:

Ok, in the index.html part you can should put:

<!DOCTYPE html>
<html>
    <head>
<title>Result</title>
<script type='text/javascript' src='script.js'></script>
    </head>
        <body></body>
</html>

And in the script.js part you should put:

$h1 = $("<h1>Hello</h1>");

OR YOU CAN PUT

 var $h1 = $("<h1>Hello</h1>");

This should definitely work :smile: :+1: (or else if it doesn't, then they're might be something other than your code that would be causing the problem) :v:


#9

If you have problem with "Did you create variable h1?", do this.

  1. Reset your code
  2. Write in script.js ---> $h1 = $("<h1>Hello</h1>");

#10

I got the same problem. But then I reset the code and typed

$h1 = $("<h1>Hello</h1>");

on the js.script and it worked!!


#11

var $h1 = $('<h1></h1>').text('Hello');