First of all, it looks like you forgot to put 'typeit("does this work?");' in a <script> (not sure if this was a typo). However, even if you do that, it will fail. Stupid mistake on my part, and here's the explanation:
The function you passed to $(document).ready will only be executed when the DOM is fully loaded. Your second script (the "does this work?" part) is trying to execute before the DOM has loaded, when the variable typeit is still undefined.
So how do we fix it? Luckily, nothing in your $(document).ready function requires the DOM. You're just creating functions. So we don't have to involve jQuery at all (which means that the last piece of advice I gave is irrelevant; if you're creating the typeit function in the global scope, then you don't have to worry about declaring the variable; you take your first script that you posted and literally just get rid of the $(document).ready(function () {}) wrapper).
Once you've done that, where are you allowed to use your typeit function? Well, because you hardcoded in the element that you want to use (the #box <div>), you have to wait until that DOM node has been created. Which just means: after the #box <div> in your HTML.
HTML Code:
<head>
<script>
// create the typeit function
</script>
</head>
<body>
<script>
// this WILL NOT work
// because the #box <div> doesn't exist yet!
typeit('hello?');
</script>
<div id="box"></div>
<script>
// this WILL work
typeit('huzzah!');
</script>
</body>
Bookmarks