# Html file vs codepen

**URL:** <https://forum.freecodecamp.org/t/html-file-vs-codepen/175832>\
**Category:** General\
**Created:** [February 21, 2018, 11:01am UTC](https://forum.freecodecamp.org/t/html-file-vs-codepen/175832 "2018-02-21T11:01:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![woodsrlee](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@woodsrlee](https://forum.freecodecamp.org/u/woodsrlee)\
**Post date:** [February 21, 2018, 11:01am UTC](https://forum.freecodecamp.org/t/html-file-vs-codepen/175832/1 "2018-02-21T11:01:35Z")

</div>

Hello everybody.

if I put everything into a html file, I get errors from the javascript loading before the html.  
so I will get getElementById(“inputbox”).value is a null value because the javascript loaded before the body.

likewise if there are vars that I need built on startup and its in the body tag it will say that vars are undefined.

another problem that I see is that on most of the projects with code pen I can put

```auto
document.getElementById("button).onclick=func();

```

and it will launch when I click the button. but when I do it from a local HTML page it will launch func() on load and not launch when I click the button.  
I have to put

```auto
document.getElementById("button).onclick=function(){func();}

```

should I set two script tags in `<head>` and `<body>`?

should I just be putting script tags in the `<body>`?  
or entirely separate or include .js file?

I feel if there is something I’m missing here.

```auto
<head>

<style>
body{
background-color:#777;
}

#results{
color:red;
width:100%;
background-color:#fff;
}
</style>
<script language="javascript">

function factors(num){

document.getElementById("results").innerHTML=""+num;

}
var x=document.getElementById("box").value;
document.getElementById("go").onclick=function(){factors(x);} 

</script>
</head>
<body>
<input id="box">
<button id="go" >GO!</button>
<div id="results">aaa</div>
</body>
current error is that value is null.

```

---

<div class="post-metadata">

**Author:** ![MrDjango](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mrdjango/32/52069_2.png) [@MrDjango](https://forum.freecodecamp.org/u/MrDjango)\
**Post date:** [February 21, 2018, 11:07am UTC](https://forum.freecodecamp.org/t/html-file-vs-codepen/175832/2 "2018-02-21T11:07:48Z")

</div>

Without going into the specifics of you code, you should put the script tags immediately before the closing tag. Try that first.

---

<div class="post-metadata">

**Author:** ![JohnnyBizzel](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/johnnybizzel/32/37661_2.png) [@JohnnyBizzel](https://forum.freecodecamp.org/u/JohnnyBizzel)\
**Post date:** [February 21, 2018, 12:47pm UTC](https://forum.freecodecamp.org/t/html-file-vs-codepen/175832/3 "2018-02-21T12:47:58Z")

</div>

You can just put the onclick directly on the button in HTML.  
Then all you need is to set the value of the output to the value of the box.  
Example: [https://codepen.io/JohnnyBizzel/pen/xYjPvg](https://codepen.io/JohnnyBizzel/pen/xYjPvg)

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [February 21, 2018, 5:13pm UTC](https://forum.freecodecamp.org/t/html-file-vs-codepen/175832/4 "2018-02-21T17:13:57Z")

</div>

Just to clarify MrDjango is talking about moving your `script` tag until just before the `</body>` tag. This is a common practice. If you put the JS in the `head` section, your JS loads before the page. But you want it to load _after_ your HTML. You want this for two reasons:

1. For exactly the reason you’ve encountered.
2. So the page doesn’t wait for JS to load before displaying HTML

Generally you want the CSS in the head (so the HTML gets styled) and put the JS at the end of the `body`. There are other approaches, but this is the most common.

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [February 21, 2018, 6:10pm UTC](https://forum.freecodecamp.org/t/html-file-vs-codepen/175832/6 "2018-02-21T18:10:56Z")

</div>

```auto
document.getElementById("button").onclick=func();

```

I’m assuming that the code you’re using in your project is properly closing the strings in `getElementById`, but in case you copied and pasted that code into your forum post, you ought to go fix that.

When you assign `func()` to a click handler (or any variable at all), you’re assigning **the output of the function func()**. The parentheses mean that you’re calling the function. If you want your click handler to _run_ the function `func`, then give it the reference without the parentheses.

```javascript
document.getElementById("button").onclick=func; // << no parens

```

---

<div class="post-metadata">

**Author:** ![woodsrlee](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@woodsrlee](https://forum.freecodecamp.org/u/woodsrlee)\
**Post date:** [February 21, 2018, 6:17pm UTC](https://forum.freecodecamp.org/t/html-file-vs-codepen/175832/7 "2018-02-21T18:17:36Z")

</div>

Okay,  
But then how would I pass a value to it? Say function(someFormData)? Would best practice be to build another function to college the data then launch it?

Like

```auto
function launcher(){
Var x=..("box").value;
function(x);
}

```

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [February 21, 2018, 6:26pm UTC](https://forum.freecodecamp.org/t/html-file-vs-codepen/175832/8 "2018-02-21T18:26:52Z")

</div>

Wrapping functions just as you have been doing is perfectly acceptable.

```auto
document.getElementById("go").onclick=function(){factors(x);} 

```

👍

It isn’t always the most readable, however, and you can quickly find yourself writing brittle code. I would get into this more, but reading your post it seems you expect the variable `x` to be the value of your element `#box` _at the time you click your button_. This is not going to happen. The reason you’re seeing a null value is because the value is null at the time this code is executed

```auto
var x=document.getElementById("box").value;

```

It doesn’t change just because the value changes. You need to get the value _each time_ you want to operate on it. Easy fix, just call that same code inside your function.

```auto
function func() {
    var x = document.getElementById("box").value;
    console.log(x); // this will show the proper value
    // do other things here
}

document.getElementById("go").onclick = func;

```

---

<div class="post-metadata">

**Author:** ![SerafimPoch](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/serafimpoch/32/39780_2.png) [@SerafimPoch](https://forum.freecodecamp.org/u/SerafimPoch)\
**Post date:** [February 21, 2018, 6:34pm UTC](https://forum.freecodecamp.org/t/html-file-vs-codepen/175832/9 "2018-02-21T18:34:34Z")

</div>

onClick is a bad practice, use addEventListener

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:26pm UTC](https://forum.freecodecamp.org/t/html-file-vs-codepen/175832/10 "2021-01-17T20:26:16Z")

</div>

Closed on January 17, 2021, 8:26pm UTC


