0

I'm stumped as to why my for loops are displaying "undefined" before any of my actual output occurs. I have all variables declared, and using Inspect Element shows no syntax errors.

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
    $(function(){ 
    "use strict";
        var html, s, vindex;
        var v = ['alpha::one::uno', 'beta::two::dos', 'gamma::three::tres'];
        for (vindex = 0; vindex < v.length; vindex++) {

            s = v[vindex].split('::');
            html += '<div class="inline ' + s[0] + '">\n';
            html += '<h4>' + s[1] + '</h4>';
            html += '<a href="javascript://" class="link">' + s[2] + '</a></div>';
        }
        $("div").append( html );
    });
</script>
</head>

<body>
<div></div>
</body>
</html>

http://jsfiddle.net/a5b6C/1/

2
  • 2
    Inspect Element allows you to explore and manipulate the DOM; it has nothing to do with JavaScript syntax errors. You're thinking of the JavaScript console. Commented Feb 14, 2014 at 21:09
  • You are right about that. Commented Feb 14, 2014 at 21:45

1 Answer 1

6

The undefined string shows up because html is undefined

var html, s, vindex;

var html; is basically the same thing as var html = undefined;

Set it as an empty string.

var html = "", s, vindex;
Sign up to request clarification or add additional context in comments.

1 Comment

Excuse me while I smack my forehead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.