0

This is probably something simple, but I can't figure it out. Thanks! http://jsfiddle.net/8DGgg/1/

  var listP ={"PEOPLE": [
       {  "name": "Joe",  "age": "1"  },
       {  "name": "Jim",  "age": "2" },
       {  "name": "Jessica",  "age": "3" }
       ]};


 for(var j in listP.PEOPLE) { 
     $("#dir").append( "<div class=\"whoWrap\">" );
     $(".whoWrap").append( listP.PEOPLE[j].name + " " + j );
     $("#dir").append("</div>");
    }

Desired result:

  <div id="dir">
      <div class="whoWrap">Joe 0</div>
      <div class="whoWrap">Jim 1</div>
      <div class="whoWrap">Jessica 2</div>
      </div>

Actual result:

      <div id="dir">
          <div class="whoWrap">Joe 0Jim 1Jessica 2</div>
          <div class="whoWrap">Jim 1Jessica 2</div>
          <div class="whoWrap">Jessica 2</div>
      </div>

5 Answers 5

2

you have to do this way:

for(var j in listP.PEOPLE) { 

 $("#dir").append( "<div class=\"whoWrap\">"+listP.PEOPLE[j].name + " " + j +"</div>" );

 $("#dir").append("</div>");

}

you don't need to append in whoWrap just append in same dir div and it will be fine.

UPDATED FIDDLE

Sign up to request clarification or add additional context in comments.

Comments

1

$(".whoWrap") selected all div's whose class matched. Hence causing the duplication. Modify your code as below.

Fiddle here : http://jsfiddle.net/CodingDawg/8DGgg/2/

for (var j in listP.PEOPLE) {
   var div = "<div class='whoWrap'>" + listP.PEOPLE[j].name + " " + j + "</div>"
   $("#dir").append(div);

 }

Comments

1

It's because the .whoWrap selector matches all previous elements that have already been appended in previous iterations. To resolve, reduce the elements using .last().

Also it would be a good idea to add #dir to the selector so it only targets elements within that.

 $("#dir > .whoWrap").last().append( listP.PEOPLE[j].name + " " + j );

The whole thing would be better written as:

$.each(listP.PEOPLE, function(index){
    $("#dir").append(
        $('<div>')
            .addClass('whoWrap')
            .text(this.name + " " + index)
    );
});

FIDDLE

  • Working with DOM elements (or jQuery objects) is better than dealing with raw HTML.
  • For in loops have issues when looping arrays, use jQuery $.each or a basic for loop.

Comments

1

Try this:

   $(document).ready(function(){

   var listP ={"PEOPLE": [{  "name": "Joe",  "age": "1" },{  "name": "Jim",  "age": "2"  },{  "name": "Jessica",  "age": "3"}]};


      for(var j in listP.PEOPLE) { 
          var whoWrap=$('<div class=\"whoWrap\"></div>');

         whoWrap.append( listP.PEOPLE[j].name + " " + j );
         $("#dir").append(whoWrap);

    }


    });

Hope it helps.

Comments

1

Yes, the problem is that you append content to this class $(".whoWrap") so It'll match all of them.

Here is your jsfiddle working http://jsfiddle.net/mrodriguezr/8DGgg/4/

$(document).ready(function(){

   var listP ={"PEOPLE": [
           {  "name": "Joe",  "age": "1" },
           {  "name": "Jim",  "age": "2"  },
           {  "name": "Jessica",  "age": "3"}
           ]};

 for(var j in listP.PEOPLE) { 
      var $whoDiv = $('<div>', {'class' : 'whoWrap'}).append(listP.PEOPLE[j].name + " " + j );

      $("#dir").append($whoDiv);
      $("#dir").append($whoDiv);
    }
});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.