Passing JSON object through loop to be used in jQuery Dialog I'm wondering if there is a better (cleaner?) method than my current implementation. I'm currently encoding a PHP SimpleXMLObject (USPS Tracking API) to JSON and looping through said JSON object via JavaScript to operate the front-end.

Examples from my current implementation below:

Function to display dialog implemented anonymously outside of .ready():

var moreInfo_popup = function(i) {
$('#moreinfo'+i).dialog({
        modal:false,
        autoOpen:false,
        height:555,
        title: 'Detailed View',
        width:500,
        draggable:false,
        buttons: {
            Ok: function(){
                $(this).dialog("close");
        }
    }
    });

        $('#moreinfo'+i).dialog('open');

        }


Main loop for displaying Tracking ID, Most Recent Event, and Mail Class for each API response- I'm currently generating a content div appended to #modal_container, then calling moreInfo_popup() inline via &lt;input onClick=""&gt;:

for(var key in obj) {
                    if(obj.hasOwnProperty(key)) {
                    if(key % 2 === 0) {
                    $('#page-nav').append("&lt;div id=\"results_table\"&gt;&lt;table class=\"data_table\"id=\"data_table_id\"border=\"0\"width=\"60%\"align=\"center\"&gt;&lt;tr&gt;&lt;td align=center width=20%&gt;"+obj[key].TrackInfo.Attributes.ID+"&lt;/td&gt;&lt;td align=\"center\"width=\"35%\"&gt;"+obj[key].TrackInfo.StatusSummary+"&lt;/td&gt;&lt;td align=\"center\"width=\"20%\"&gt;"+obj[key].TrackInfo.Class+"&lt;/td&gt;&lt;td align=\"center\"&gt;&lt;input type=\"button\"class=\"moreInfo\"value=\"Detail\"id=\"_buttonMoreInfo\"onClick=\"moreInfo_popup("+key+")\"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;");

                    $('#modal_container').append("&lt;div id=\"moreinfo" + key + "\"&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;" + obj[key].TrackInfo.Attributes.ID +"&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;").hide();
                  }
                else {
                  $('#page-nav').append("&lt;div id=\"results_table\"&gt;&lt;table class=\"data_table_even\" id=\"data_table_id\" border=0 width=60% align=center&gt;&lt;tr&gt;&lt;td align=center width=20%&gt;" 
                                      + obj[key].TrackInfo.Attributes.ID + "&lt;/td&gt;&lt;td align=center width=35%&gt;" + obj[key].TrackInfo.StatusSummary + "&lt;/td&gt;&lt;td align=center width=20%&gt;" 
                                      + obj[key].TrackInfo.Class + "&lt;/td&gt;&lt;td align=\"center\"&gt;&lt;input type=\"button\" value=\"Detail\" class=\"moreInfo\" id=\"_buttonMoreInfo\"onClick=\"moreInfo_popup("+key+")\"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;");

                  $('#modal_container').append("&lt;div id=\"moreinfo" + key + "\"&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;" + obj[key].TrackInfo.Attributes.ID +"&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; &lt;button&gt;OK&lt;/button&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;");


                }

            }

      $("#page-nav td:contains('undefined')").html("Invalid");    
  }


As I'm sure you can see, this feels like an incredibly tedious way of accomplishing the desired outcome, to which there is surely a better alternative. As a newcomer to JavaScript/jQuery, I've done plenty of searching on this subject, but haven't really understood much of what I found, if indeed I was asking the right questions in the first place.
The key problem I have with your code right now is that it doesn't separate data from presentation. This is a maintainability issue because it means now your data is inexplicably tied to your view, and you cannot change one without altering the other. How can we fix this?

As someone earlier suggested, you could use a template library. When I'm doing stand-alone development (i.e, not using a framework), I really like rivets. It's lightweight (6.2kb), simple and unopinionated. 

I would have recommended dropping jQuery, but I see that you're using a plugin for jQuery, so I can't really suggest that. I would say that if you can find a way to avoid using jQuery here, that would be great. The reason I don't want you using jQuery is because it's a heavyweight library; it's 34kb, which is over 5x the size of Rivets.

The time-to-last-byte is very important in JavaScript development because until your consumer has received the last byte, your JavaScript is doing nothing, which potentially makes part of your site unusuable. As such, if you can remove any cruft from your site to make it load all of the script tags faster, the better off you will be.

First, we'll define a HTML layout for your tracking info. As your tracking info is tangential to the current information, we can make this use the &lt;aside&gt; HTML element.

&lt;aside&gt;
  &lt;table&gt;
    &lt;tr&gt;
      &lt;td&gt;ID&lt;/td&gt;
      &lt;td&gt;Summary&lt;/td&gt;
      &lt;td&gt;Class&lt;/td&gt;
      &lt;td&gt;
        &lt;button&gt;Detail&lt;/button&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
&lt;/aside&gt;


So above is the basic structure of your dialog. There's a lot of inline styling, and the only thing your "if" statement is for is to add alternating classes. No need to do that here! We can do that in CSS just fine. First of all, lets apply your styles with a bit of CSS. We'll add a .data-table class to your table, although I would definitely recommend naming it something more descriptive; .data-table is pretty ambigious. You want to describe the role this thing fulfills, not what it is. In other words, be more declarative with your css names. In addition, we'll add a .results-table class to the encapsulating &lt;aside&gt;. Whilst I'm not sure this is completely correct, it appears to be closest to what you are doing.

First, let's tackle the overarching styles of each of the tables:

.data-table {
  border: 0;
  width: 60%;
  align: center;
}


You'll notice I have not included the &lt;td&gt; styling. This is because the width of a &lt;td&gt; is decided by the browser and not the CSS. This makes sense if you are using truly tabular data. If you're not using tabular data (and I suspect you're not), then you should use a &lt;dl&gt;. &lt;dl&gt; are definition lists which may be more suited for this sort of thing.

You appear to have some kind of alternate styling based on whether or not a .data-table is an even .data-table. I'm not sure what this styling is because it isn't supplied, but you can replicate this class by using the following CSS selector:

.results-table:nth-child(even) {
  // some CSS styling here
}


So far, we have this:

&lt;aside class='results-table'&gt;
  &lt;table class='data-table'&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td&gt;
        &lt;button&gt;Detail&lt;/button&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
&lt;/aside&gt;


Now, your data model looks something like this:

var model = {
  someNumericValue: {
    TrackInfo: {
      Attributes: {
        ID: 0
      },
      StatusSummary: "summary",
      Class: "first"
    }
  }
};


Wow, this looks a lot like an array! It might be, or it might not be. It's hard to tell because arrays are actually objects. This means that they have access to the hasOwnProperty of an object. Lets assume it is an array because that makes more sense.

var models = [
  { 
    TrackInfo: {
      Attributes: {
        ID: 0
      },
      StatusSummary: "summary",
      Class: "first"
    }
  }
];


With this data model, we can easily slot this into our template using rivets bindings. Lets take a look at how it pans out (I've skipped the popup at the moment, we'll get into that in a bit).

&lt;aside class='results-table'&gt;
  &lt;table class='data-table' rv-each-order='model.orders'&gt;
    &lt;tr&gt;
      &lt;td rv-text='order.TrackInfo.Attributes.ID'&gt;&lt;/td&gt;
      &lt;td rv-text='order.TrackInfo.StatusSummary'&gt;&lt;/td&gt;
      &lt;td rv-text='order.TrackInfo.Class'&gt;&lt;/td&gt;
      &lt;td&gt;
        &lt;button&gt;Detail&lt;/button&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
&lt;/aside&gt;


Now when we set up rivets on this element, as long as we pass in all of our models to "models" - voila, you'll have data binding. Of course, this isn't 100% perfect because we're still relying on our JavaScript to populate the data, however it's a good start; the HTML structure is there and it's declarative. Anyone reading over this file with some knowledge of how rivets works would be able to tell what this does, and the HTML isn't hidden away in JavaScript - the only thing we're getting from JavaScript is the repeating behaviour and the values. This is a good shift!

How would you use this, you might ask? Well, to bind the &lt;aside&gt; to data, it really is as simple as this:

var dialog = document.querySelector('aside.results-table');
rivets.bind(dialog, { order: models });


Where, obviously, models is your array of data. This will ensure that all of your data is bound correctly. Note that this is only one-way data binding though, so any changes you make (DOM -> Model, such as a form) will not be reflected in the change to the view - only model changes will update the view.

Alright, so we have the basic setup done. Now, how do we tackle the fact that we have a pop up that is displayed as well? Well, this gets a bit tricky because you're using a jQuery plugin. It's going to be difficult to neatly display a popup based on DOM elements without doing some nasty fudging. The easiest way would be to continue using jQuery like you're doing now. However, this presents some nasty stuff, namely that you're using numbered IDs to access the dialogs. Ouch! Instead, I would suggest creating a Dialog in HTML and not using JQuery. Like this:

&lt;dialog&gt;
  &lt;title&gt;Detailed View&lt;/title&gt;
  &lt;table&gt;
    &lt;tr&gt;
      &lt;td rv-text='order.TrackInfo.Attributes.ID'&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;button&gt;OK&lt;/button&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
&lt;/dialog&gt;


Note that the &lt;dialog&gt; element is a HTML5 element, so you may want to change this to a &lt;div&gt; if you need to support browsers that aren't HTML5. However, given that I've used &lt;aside&gt; and &lt;section&gt; you would need to do that for everything else as well. Worst case scenario, use a HTML5 polyfill. Let's put this all together and add the functionality for the pop-up to be closed/opened after the button is clicked..

&lt;aside class='results-table'&gt;
  &lt;section rv-each-order='orders'&gt;
    &lt;table class='data-table'&gt;
      &lt;tr&gt;
        &lt;td rv-text='order.TrackInfo.Attributes.ID'&gt;&lt;/td&gt;
        &lt;td rv-text='order.TrackInfo.StatusSummary'&gt;&lt;/td&gt;
        &lt;td rv-text='order.TrackInfo.Class'&gt;&lt;/td&gt;
        &lt;td&gt;
            &lt;button rv-on-click='showMoreInfo'&gt;Detail&lt;/button&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
    &lt;dialog rv-if='order.displayMoreInfo'&gt;
      &lt;title&gt;Detailed View&lt;/title&gt;
      &lt;table&gt;
        &lt;tr&gt;
          &lt;td rv-text='order.TrackInfo.Attributes.ID'&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;
            &lt;button rv-on-click='hideMoreInfo'&gt;OK&lt;/button&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
      &lt;/table&gt;
    &lt;/dialog&gt;
  &lt;/section&gt;
&lt;/aside&gt;


Note how I've moved the rv-each-order to a &lt;section&gt; that encapsulates both the &lt;table&gt; and the &lt;dialog&gt;. This makes it easier to group the elements together and better represents that they are a single, isolated section of content compared to the other orders you've put in here. On clicking of the "Detail" button, the dialog will be shown. When the dialog "OK" button is clicked, the dialog will be hidden. Simple stuff, right? (Note: Shown and hidden is not a good analogy. What actually happens is that the entire &lt;dialog&gt; will be removed/added to the DOM. If you wish to instead actually show/hide the &lt;dialog&gt; - for performance reasons, maybe - then use rv-show/rv-hide).

Now, lets add the final bootstrapping logic..

function hideMoreInfo(event, context) {
  var currentOrder = context.order;
  currentOrder.displayMoreInfo = false;
}

function showMoreInfo(event, context) {
  var currentOrder = context.order;
  currentOrder.displayMoreInfo = true;
}

var resultsTable = document.querySelector('.results-table');
rivets.bind(resultsTable, {
  hideMoreInfo: hideMoreInfo,
  showMoreInfo: showMoreInfo,
  orders: models
});


That's it - we're done with the logic side of things! The only thing left would be to do CSS. Unfortunately, I can't do more CSS than what I've done for you because you haven't actually linked any, but if you would like some more help on tht please let me know and I'll gladly oblige.

To summarize, here's the finalised code:

HTML


&lt;aside class='results-table'&gt;
  &lt;section rv-each-order='orders'&gt;
    &lt;table class='data-table'&gt;
      &lt;tr&gt;
        &lt;td rv-text='order.TrackInfo.Attributes.ID'&gt;&lt;/td&gt;
        &lt;td rv-text='order.TrackInfo.StatusSummary'&gt;&lt;/td&gt;
        &lt;td rv-text='order.TrackInfo.Class'&gt;&lt;/td&gt;
        &lt;td&gt;
            &lt;button rv-on-click='showMoreInfo'&gt;Detail&lt;/button&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
    &lt;dialog rv-if='order.displayMoreInfo'&gt;
      &lt;title&gt;Detailed View&lt;/title&gt;
      &lt;table&gt;
        &lt;tr&gt;
          &lt;td rv-text='order.TrackInfo.Attributes.ID'&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;
            &lt;button rv-on-click='hideMoreInfo'&gt;OK&lt;/button&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
      &lt;/table&gt;
    &lt;/dialog&gt;
  &lt;/section&gt;
&lt;/aside&gt;


JavaScript


function hideMoreInfo(event, context) {
  var currentOrder = context.order;
  currentOrder.displayMoreInfo = false;
}

function showMoreInfo(event, context) {
  var currentOrder = context.order;
  currentOrder.displayMoreInfo = true;
}

var resultsTable = document.querySelector('.results-table');
rivets.bind(resultsTable, {
  hideMoreInfo: hideMoreInfo,
  showMoreInfo: showMoreInfo,
  orders: models
});


CSS


.data-table {
  border: 0;
  width: 60%;
  align: center;
}