0
$.ajax({
      url: 'db_employer_info_lookup.php',
      type: 'POST',
      dataType: 'json'

  }).done(function(data) {
        // console.log(data.employee_info[0].length);
        for (var i = 0; i < data.employee_info[0].length; i++) {
          if(i%2 == 0){
            $('#the-team-wrapper .flex-content').append('<div class="employee-profile-pic flex-item" data-img-src="'
            +data.employee_info[0][i]['profile_pic']+'"></div><div class="employee-bio-wrapper flex-item"><h2>'
            +data.employee_info[0][i]['firstname']+" "+data.employee_info[0][i]['lastname']+'</h2>'+$('<h3 />')
            .html(get_job_title(data.employee_info[0][i]['id']))+'<p>'+data.employee_info[0][i]['bio']+
            '</p></div>');
          }else {
            $('#the-team-wrapper .flex-content').append('<div class="employee-bio-wrapper flex-item"><h2>'
            +data.employee_info[0][i]['firstname']+" "+data.employee_info[0][i]['lastname']+'</h2>'+**$('<h3 />')
            .html(get_job_title(data.employee_info[0][i]['id']))**+'<p>'+data.employee_info[0][i]['bio']
            +'</p></div><div class="employee-profile-pic flex-item" data-img-src="'
            +data.employee_info[0][i]['profile_pic']+'"></div>');
          }

          var profile_pic_path = data.employee_info[0][i]['profile_pic'].split('\\').join('\\\\');
          $("#the-team-wrapper .flex-content-wrapper .flex-content .employee-profile-pic:eq("+i+")")
          .css({'background': 'url(_employee_pics/'+profile_pic_path+')',
          'background-repeat': 'no-repeat','background-position': 'center', 'background-size': 'cover'});
        }

      });



      var get_job_title = function(employee_id){
        $.ajax({
          url: 'db_job_title_lookup.php',
          type: 'POST',
          dataType: 'text',
          data:{
            employee_id: employee_id
          }
        }).done(function(data1){
          data1;
      });
      };

Hi, I have this code where I'm retrieving some data from the server using Ajax in the 'var get_job_title' function. Now I want to use that text in an H3 element as seen in the code above the 'var get_job_title' function but all I get back is [object Object]. Any help please!!!

  • 1
    You can not return from an asynchronous call. – epascarello Oct 05 '16 at 18:23
  • 1
    why can't you just have `.append('....

    ' + yourvar + '

    ....')`? You're already stuffing a ton of html into there with plain string operations, so what's the mental block keeping you from doing the same with the h3?
    – Marc B Oct 05 '16 at 18:23
  • @MarcB thats what I was doing but that was returning undefined, What I'm trying to do is use a value from the returned data in the ajax request that the H3 lives, to pass to a function that does another ajax request with that data and return the text to me. Is that possible. hope it makes sense. – Patrick Traile Oct 05 '16 at 18:33
  • @epascarello what would be a good solution for what I'm trying to do? – Patrick Traile Oct 05 '16 at 18:38
  • Best thing would be that the server do the lookups so you do not need to do it on the client. http requests are slow and you will be making a lot of them when it could have been done in just one. If you have to do it on the client, you probably should process the JSON first to get the values, than build the HTML. – epascarello Oct 05 '16 at 18:47

0 Answers0