Question

Yesterday I was looking for a text effect using javascript. I was pointed to this fiddle: http://jsfiddle.net/vCx6W/1/ and this is where it was posted: https://stackoverflow.com/questions/4074399/what-to-choose-for-typewriter-effect-in-javascript

I am trying to use the same thing. I added the javascript like this:

 <script type="text/javascript" src="http://code.jquery.com/
 jquery-1.4.2.min.js"></script>

 <script type="text/javascript">
 $.fn.teletype = function(opts) {
 var $this = this,
    defaults = {
        animDelay: 50
    },
    settings = $.extend(defaults, opts);

 $.each(settings.text, function(i, letter) {
    setTimeout(function() {
        $this.html($this.html() + letter);
    }, settings.animDelay * i);
 });
 };

 $(function() {
 $('#container').teletype({
    animDelay: 100,
    text: 'Now is the time for all good men to come to the aid of their country...'
 });
 });
 </script>

And this in the HTML:

 <div id="container"></div>

However, I am unable to make the text appear at all. What am I doing wrong?

Was it helpful?

Solution

Perhaps use the correct jQuery framework?

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>

Also remove the breakline after http://code.jquery.com/... ;)

Like this:

<html>
    <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>

     <script type="text/javascript">
     $.fn.teletype = function(opts) {
     var $this = this,
        defaults = {
            animDelay: 50
        },
        settings = $.extend(defaults, opts);

     $.each(settings.text, function(i, letter) {
        setTimeout(function() {
            $this.html($this.html() + letter);
        }, settings.animDelay * i);
     });
     };

     $(function() {
     $('#container').teletype({
        animDelay: 100,
        text: 'Now is the time for all good men to come to the aid of their country...'
     });
     });
     </script>

    </head>
    <body>

       <div id="container"></div>
    </body>
</html>

OTHER TIPS

My guess this isn't a problem with the javascript, but with loading the required resources.

I'm unable to load jquery via the url you provided:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>'

An easy way to find this is with your browser's developer tools (I use Chrome). When I open a page with this code in Chrome, press f12 to open the developer tools, and check the console, I get the error 'Failed to load resource: the server responded with a status of 404 (Not Found) for http://code.jquery.com/%20jquery-1.4.2.min.js

Also, I noticed the fiddle is using jQuery 1.7.2. If you're still having problems, try 1.7.2 instead of 1.4.2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top