title: Paste json in a textarea and convert to formatted html with jquery
body: <p>The way it should work is</p>
  
  <ol>
  <li>Paste json into textarea "jsonvalue"</li>
  <li>Click "convert"</li>
  <li>Formatted html appears in "placeholder"</li>
  </ol>
  
  <p>I am trying to get the title and url out of the json and instead when I click on "convert" button, I get - <em>Uncaught TypeError: Cannot read property 'length' of undefined</em> </p>
  
  <p>Link to <strong><a href="http://jsfiddle.net/sBLyr/" rel="nofollow noreferrer">jsFiddle</a></strong></p>
  
  <p>This is the html and js</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  
  
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
  &lt;meta http-equiv="Content-Language" content="he" /&gt;    &lt;title&gt;JSON Sample&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;textarea id="jsonvalue" style="width:100%" rows="1"&gt;&lt;/textarea&gt;
  &lt;button id="convert"&gt;Click here to convert!&lt;/button&gt;
  &lt;div id="placeholder"&gt;&lt;/div&gt;
  
  
  
      &lt;script src="http://code.jquery.com/jquery-1.9.1.min.js"&gt;&lt;/script&gt;
      &lt;script&gt;
  
  $(document).ready(function () {
      $("#convert").click(function () {
  
          var output = '&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;head&gt;&lt;meta http-equiv="Content-Type" content="text/html; 
  
  charset=UTF-8" /&gt;&lt;title&gt;Instapaper: Export&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Unread&lt;/h1&gt;&lt;ol&gt;&lt;/ol&gt;&lt;h1&gt;Archive&lt;/h1&gt;&lt;ol&gt;';
  
          var data = $('#jsonvalue').val();
  
  
  
          $.each(data.items, function (i, items) {
              output += '&lt;li&gt;&lt;a href="' + items.alternate[0].href + '"&gt;' + items.title + '&lt;/li&gt;';
          });
  
          output += '&lt;/ol&gt;&lt;/body&gt;&lt;/html&gt;';
          $('#placeholder').text(output);
      });
  
  });
  
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is the json I paste in the textarea</p>
  
  <pre><code>{
    "id" : "user/17115776656148603551/state/com.google/starred",
    "title" : "Dasa סימן פריטים בכוכב ב-Google Reader",
    "author" : "Dasa",
    "updated" : 1368699933,
    "direction" : "ltr",
    "items" : [ {
      "crawlTimeMsec" : "1368699933957",
      "timestampUsec" : "1368699933957939",
      "id" : "tag:google.com,2005:reader/item/eb307953496c3755",
      "categories" : [ "user/17115776656148603551/state/com.google/starred", "user/17115776656148603551/state/com.google/fresh", "search", "job search", "news stories", "trending", "job hunting", "news", "vanity searches", "productivity", "search query", "google alerts", "alerts", "organization", "local news", "google", "time savers", "trending news", "shortcuts", "notifications" ],
      "title" : "Five Creative Uses for Google Alerts",
      "published" : 1366282800,
      "updated" : 1366282800,
      "canonical" : [ {
        "href" : "http://lifehacker.com/five-creative-uses-for-google-alerts-475278626"
      } ],
      "alternate" : [ {
        "href" : "http://feeds.gawker.com/~r/lifehacker/full/~3/jaLbxODg6SM/five-creative-uses-for-google-alerts-475278626",
        "type" : "text/html"
      } ],
      "summary" : {
        "direction" : "ltr",
        "content" : "&lt;p&gt;&lt;img height=\"360\" width=\"640\" src=\"http://img.gawkerassets.com/img/18kye9icys64djpg/ku-xlarge.jpg\"&gt;&lt;/p&gt;&lt;p&gt;Google Alerts is one of Google's hidden gems. It's a really powerful tool to keep track of trends, interesting topics, or anything really new that appears on the web. If you're not using it already, here are a few creative ways to get started with it.&lt;/p&gt;&lt;p&gt;&lt;a href=\"http://lifehacker.com/five-creative-uses-for-google-alerts-475278626\"&gt;Read more...&lt;/a&gt;&lt;/p&gt;&lt;img width=\"1\" height=\"1\" src=\"http://lifehacker.feedsportal.com/c/34977/f/647165/s/2add7ec3/mf.gif\" border=\"0\"&gt;&lt;div&gt;&lt;table border=\"0\"&gt;&lt;tr&gt;&lt;td valign=\"middle\"&gt;&lt;a href=\"http://share.feedsportal.com/share/twitter/?u=http%3A%2F%2Flifehacker.com%2Ffive-creative-uses-for-google-alerts-475278626&amp;amp;t=Five+Creative+Uses+for+Google+Alerts\"&gt;&lt;img src=\"http://res3.feedsportal.com/social/twitter.png\" border=\"0\"&gt;&lt;/a&gt; &lt;a href=\"http://share.feedsportal.com/share/facebook/?u=http%3A%2F%2Flifehacker.com%2Ffive-creative-uses-for-google-alerts-475278626&amp;amp;t=Five+Creative+Uses+for+Google+Alerts\"&gt;&lt;img src=\"http://res3.feedsportal.com/social/facebook.png\" border=\"0\"&gt;&lt;/a&gt; &lt;a href=\"http://share.feedsportal.com/share/linkedin/?u=http%3A%2F%2Flifehacker.com%2Ffive-creative-uses-for-google-alerts-475278626&amp;amp;t=Five+Creative+Uses+for+Google+Alerts\"&gt;&lt;img src=\"http://res3.feedsportal.com/social/linkedin.png\" border=\"0\"&gt;&lt;/a&gt; &lt;a href=\"http://share.feedsportal.com/share/gplus/?u=http%3A%2F%2Flifehacker.com%2Ffive-creative-uses-for-google-alerts-475278626&amp;amp;t=Five+Creative+Uses+for+Google+Alerts\"&gt;&lt;img src=\"http://res3.feedsportal.com/social/googleplus.png\" border=\"0\"&gt;&lt;/a&gt; &lt;a href=\"http://share.feedsportal.com/share/email/?u=http%3A%2F%2Flifehacker.com%2Ffive-creative-uses-for-google-alerts-475278626&amp;amp;t=Five+Creative+Uses+for+Google+Alerts\"&gt;&lt;img src=\"http://res3.feedsportal.com/social/email.png\" border=\"0\"&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;br&gt;&lt;br&gt;&lt;a href=\"http://da.feedsportal.com/r/163644809562/u/49/f/647165/c/34977/s/2add7ec3/kg/342/a2.htm\"&gt;&lt;img src=\"http://da.feedsportal.com/r/163644809562/u/49/f/647165/c/34977/s/2add7ec3/kg/342/a2.img\" border=\"0\"&gt;&lt;/a&gt;&lt;img width=\"1\" height=\"1\" src=\"http://pi.feedsportal.com/r/163644809562/u/49/f/647165/c/34977/s/2add7ec3/kg/342/a2t.img\" border=\"0\"&gt;&lt;img src=\"http://feeds.feedburner.com/~r/lifehacker/full/~4/jaLbxODg6SM\" height=\"1\" width=\"1\"&gt;"
      },
      "author" : "Alan Henry",
      "comments" : [ ],
      "annotations" : [ ],
      "origin" : {
        "streamId" : "feed/http://lifehacker.com/index.xml",
        "title" : "Lifehacker",
        "htmlUrl" : "http://lifehacker.com"
      }
    }, {
      "crawlTimeMsec" : "1368699816215",
      "timestampUsec" : "1368699816215241",
      "id" : "tag:google.com,2005:reader/item/84c25c636b707078",
      "categories" : [ "user/17115776656148603551/state/com.google/read", "user/17115776656148603551/state/com.google/starred", "user/17115776656148603551/state/com.google/fresh", "Breakfast", "Gluten-Free", "Photo", "Pudding" ],
      "title" : "yogurt panna cotta with walnuts and honey",
      "published" : 1366988860,
      "updated" : 1366988860,
      "canonical" : [ {
        "href" : "http://smittenkitchen.com/blog/2013/04/yogurt-panna-cotta-with-walnuts-and-honey/"
      } ],
      "alternate" : [ {
        "href" : "http://feedproxy.google.com/~r/smittenkitchen/~3/t_w8asbkyNg/",
        "type" : "text/html"
      } ],
      "content" : {
        "direction" : "ltr",
        "content" : "&lt;p&gt;&lt;a href=\"http://smittenkitchen.com/blog/2013/04/yogurt-panna-cotta-with-walnuts-and-honey/\" title=\"yogurt panna cotta with walnuts and honey\"&gt;&lt;img src=\"http://farm9.staticflickr.com/8400/8682467912_efaa762c89.jpg\" width=\"500\" height=\"333\" alt=\"yogurt panna cotta with walnuts and honey\"&gt;&lt;/a&gt;&lt;/p&gt;\nGuys, I just discovered the ultimate weekend brunch treat/decadent dessert that still contains a whiff of moderation/&lt;a href=\"http://www.flickr.com/photos/smitten/8681658077/in/photostream\"&gt;preschooler&lt;/a&gt; snack. The ingredient list is so short, and the cooking process is so simple that you’ll have the recipe memorized by the time you make it the second time. And you will make it a second time, maybe even within a week. It looks pretty, tastes luxurious and… well, most of you probably discovered panna cotta a decade ago.\n&lt;p&gt;&lt;a href=\"http://www.flickr.com/photos/smitten/8682466230/\" title=\"lemon, gelatin, sugar, milk/cream, yogurt\"&gt;&lt;img src=\"http://farm9.staticflickr.com/8395/8682466230_9e19d63848.jpg\" width=\"500\" height=\"333\" alt=\"lemon, gelatin, sugar, milk/cream, yogurt\"&gt;&lt;/a&gt;&lt;br&gt;\n&lt;a href=\"http://www.flickr.com/photos/smitten/8682466352/\" title=\"thick greek yogurt\"&gt;&lt;img src=\"http://farm9.staticflickr.com/8262/8682466352_5a3120eb08.jpg\" width=\"500\" height=\"333\" alt=\"thick greek yogurt\"&gt;&lt;/a&gt;&lt;/p&gt;\n&lt;p&gt;I’m sorry, I’m just slow. For example, this week I started reading &lt;a href=\"http://rads.stackoverflow.com/amzn/click/1594483299"&gt;this new book&lt;/a&gt; that everyone was talking about in September … 2007. And that’s just the beginning. Gallery wall? Skinny jeans? Arrested Development? Quinoa? People, I am &lt;i&gt;on it&lt;/i&gt;. True to sluggish form, it’s been a full four years since my friend Nicole gushed to me about the wonders of yogurt panna cotta. I put it on my cooking to-do list, blinked, and that about brings us up to last week when I saw it on my list and thought, “right, wasn’t I going to make that a few days ago?”&lt;/p&gt;\n&lt;p&gt;&lt;a href=\"http://www.flickr.com/photos/smitten/8681355169/\" title=\"yogurt whisked with milk or cream\"&gt;&lt;img src=\"http://farm9.staticflickr.com/8522/8681355169_9657d4a377.jpg\" width=\"500\" height=\"333\" alt=\"yogurt whisked with milk or cream\"&gt;&lt;/a&gt;&lt;/p&gt;\n&lt;p&gt;&lt;b&gt;... Read the rest of &lt;a href=\"http://smittenkitchen.com/blog/2013/04/yogurt-panna-cotta-with-walnuts-and-honey/\"&gt;yogurt panna cotta with walnuts and honey&lt;/a&gt; on &lt;a href=\"http://smittenkitchen.com\"&gt;smittenkitchen.com&lt;/a&gt;&lt;/b&gt;&lt;/p&gt;\n&lt;hr&gt;\n&lt;p&gt;&lt;small&gt;© smitten kitchen 2006-2012. |\n&lt;a href=\"http://smittenkitchen.com/blog/2013/04/yogurt-panna-cotta-with-walnuts-and-honey/\"&gt;permalink to &lt;b&gt;yogurt panna cotta with walnuts and honey&lt;/b&gt;&lt;/a&gt; | &lt;a href=\"http://smittenkitchen.com/blog/2013/04/yogurt-panna-cotta-with-walnuts-and-honey/#comments\"&gt;220 comments&lt;/a&gt; to date | see more: &lt;a href=\"http://smittenkitchen.com/blog/category/breakfast/\" title=\"View all posts in Breakfast\" rel=\"category tag\"&gt;Breakfast&lt;/a&gt;, &lt;a href=\"http://smittenkitchen.com/blog/category/gluten-free/\" title=\"View all posts in Gluten-Free\" rel=\"category tag\"&gt;Gluten-Free&lt;/a&gt;, &lt;a href=\"http://smittenkitchen.com/blog/category/photo/\" title=\"View all posts in Photo\" rel=\"category tag\"&gt;Photo&lt;/a&gt;, &lt;a href=\"http://smittenkitchen.com/blog/category/pudding/\" title=\"View all posts in Pudding\" rel=\"category tag\"&gt;Pudding&lt;/a&gt;\n&lt;/small&gt;&lt;/p&gt;"
      },
      "author" : "deb",
      "comments" : [ ],
      "annotations" : [ ],
      "origin" : {
        "streamId" : "feed/http://feeds.feedburner.com/smittenkitchen",
        "title" : "smitten kitchen",
        "htmlUrl" : "http://smittenkitchen.com"
      }
    } ]
  }
  </code></pre>