1

I am trying to dynamically display some data in an array that is returned from an ajax request. I know i have successfully retrieved the array from the server. My problem appears to be displaying the array on screen. I think I am making a mistake with designating the id of an element using a variable, and then trying to select for that ID with jquery. I have tried all sorts of combinations (playing with the single and double quotations ) as suggested by similar posts but none of them seem to work. Please can someone advise?

my code:

   success: function(data){
      $.each(data, function(i, element){
          // display 'filename' and corresponding 'title'.
          var id_name = element['filename'];
          mainDiv.append('<div class="wrapper_div" id='+id_name+'></div>');

          $('#' + id_name).append('<input type="checkbox" name="checkvalue[]" class="check_box" disabled="disabled" value='+element['filename']+'>');
          $('#' + id_name).append('<div>' + element['filename'] + '</div>');
          $('#' + id_name).append('<div>' + element['title'] +  '</div>');

         });
        }

I know my data is there as the following code works. The items are displayed. But i really need the above code:

   mainDiv.append('<input type="checkbox" name="checkvalue[]" class="check_box" disabled="disabled" value='+element['filename']+' >');
          mainDiv.append('<div>' + element['filename'] + '</div>');
          mainDiv.append('<div>' + element['title'] +  '</div>');
2
  • 1
    look at your brackets carefully, try "<div>" + element['filename'] + "</div>", "<div>" + element['title'] + "</div>", '<input type="checkbox" name="checkvalue[]" class="check_box" disabled="disabled" value='+element["filename"]+' >'
    – Andrew Evt
    Commented Apr 8, 2015 at 18:54
  • 1
    id='+id_name+' should be id="'+id_name+'", Your ID isn't being created as you expect it to be
    – Duane
    Commented Apr 8, 2015 at 19:09

2 Answers 2

2

Update your mainDiv.append to this

mainDiv.append('<div class="wrapper_div" id="' + id_name + '"></div>');

Also this line

$('#' + id_name).append('<input type="checkbox" name="checkvalue[]" class="check_box" disabled="disabled" value= "' + element['filename'] +'">');
2
  • i've modified my code to yours but it is still not working. I have also updated my post.
    – dave
    Commented Apr 8, 2015 at 18:46
  • 2
    Could you take the time to explain why? That way people can learn something useful from the answer that they might apply to other, similar, situations. Commented Apr 8, 2015 at 18:47
2

Are you closing out the $.each() correctly? I don't see the closing bracket and parenthesis in your code sample. It runs as expected when I add the closing bracket and parenthesis.

this pen shows the function executing correctly, so your issue may be with something else in your code.

 success: function(data){
      $.each(data, function(i, element){
          // display 'filename' and corresponding 'title'.
          var id_name = element['filename'];
          mainDiv.append('<div class="wrapper_div" id='+id_name+'></div>');

          $('#' + id_name).append('<input type="checkbox" name="checkvalue[]" class="check_box" disabled="disabled" value='+element['filename']+'>');
          $('#' + id_name).append('<div>' + element['filename'] + '</div>');
          $('#' + id_name).append('<div>' + element['title'] +  '</div>');
      });
 }
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.