Another Basic Javascript Question
<p>Ok, I have a jquery .post returning what I need it to, but it is not being output properly.</p>

<p>Here is my code:</p>

<pre><code>jQuery(document).ready(function($){


 $('#project-for-support-ticket').change(function(){  
  //Pass The value selected to the get_users_from_project_id() function
  var projectSelected = $(this).val();
  if(projectSelected == 'NULL'){
   $('#users-of-project').empty();
  }

  $.post( '/wp-admin/admin-ajax.php', { 'selectedProject' :  projectSelected, 'action' : 'support_ticket_ajax'},
                function(data) {
     var newdata = jQuery.parseJSON( data );
     var value = 'ID';
     var text = 'name';
     returnOptions(newdata, '#users-of-project', value, text);
                }, "JSON"
            );

 })

 $('#user-to-assign-project').change(function(){
  var UserSelected = $(this).val();
  if(UserSelected == 'none'){
   $('#user-type-to-assign-as').empty();
  }

  $.post( '/wp-admin/admin-ajax.php', { 'UserSelected' :  UserSelected, 'action' : 'assign_project_ajax'},
                function(data) {
     var newdata = jQuery.parseJSON( data );
     var value = '';
     var text = '';
     returnOptions(newdata, '#user-type-to-assign-as', value, text);
                }, "JSON"
         );
 })

 function returnOptions(newdata, SelectToPopulate, value, text){
  //Empty the select box so we are not simply adding more to it!
  $(SelectToPopulate).empty();

  //Run a for loop, the closest thing javascript easily has to a foreach loop
  for ( var i in newdata ){
   if(!(value == '')){
    OptVal = newdata[i].value;
   }else{
    OptVal = newdata[i];
   }
   alert (OptVal);
   if(!(text == '')){
    OptText = newdata[i].text;
   }else{
    OptText = newdata[i];
   }

   //And Append an 'Option' to the select tag
   $(SelectToPopulate).append($('&lt;option&gt;&lt;/option&gt;').attr('value', OptVal ).text(OptText));
  }
 }
    })
</code></pre>

<p>The problem is in the returnOptions function, The user-to-assign-project is working perfectly, but for project-for-support-ticket I am getting 'undefined' when I alert OptVal, but if I alert newData[i].name it alerts the correct value.</p>

<p>Can anyone see what I am doing wrong??</p>

<p>Cheers
Ben</p>
