0

I want to append the data only into a specific ID myID. It only prints the last value of the loop which is 3.

 setInterval(sample, 2000);
 function sample()
 {
   for(var i=0;i<=3;i++)
     {
         $('.found .find').each(function() {
             if(this.id == "myID")
              {
                // if the ID of this element is equal to #myID
                // this is the place where the data will append
                $(this).empty();
                $(this).append(i);
              }
         });
     }

 }

HTML:

<div class="found">
      <div class="find" id="myID"></div>
</div>

 <div class="found">
      <div class="find" id="anID"></div>
</div>

<div class="found">
      <div class="find" id="anID2"></div>
</div>
12
  • "It works for me but it ONly prints THe last value of the loop which is 3" Try not calling .empty() , which would remove previously appended i Commented May 13, 2015 at 18:03
  • @guest271314 If i get rid of .empty() it will not stop in appending data. Infinity Commented May 13, 2015 at 18:05
  • Is requirement to append 0, wait two seconds, append 1, up to 3, then start again ? Or, append 0123, 0123 infinitely ? Commented May 13, 2015 at 18:09
  • 2
    This is a very backward way to achieve what you are looking for. After reading the comments, I'm unsure as to why you are asking for "numbers instead of strings", why you cant just append the value "0123" to your div directly $('#myDiv').val("0123");, why you are confused about the fact that .empty() is causing only the last value 3 to appear, why you need to loop through all of the .found .find divs when theres a unique id.. maybe some clarification is in order? Commented May 13, 2015 at 18:26
  • 1
    "because it is an array. An array from php." Why not mentioned at actual text of Question ? See stackoverflow.com/help/how-to-ask Commented May 13, 2015 at 18:35

4 Answers 4

3

empty removes all children from the given element, so you probably want to use it before the loop:

$('.found').empty();
for (var i=0; i <= 3; i++) {
  $('.found').append(i);
}

This will empty out the container, then append your list of elements (or numbers).

This can be used in an MVC framework's render method to empty the container of the previous render before adding new content.

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

3 Comments

Thank you man. But what if I have many .found class and Inside of each class has another div element with different ID? And I want the data to append only in the specific ID? PLease help me.
@KeanAllen I'm not quite sure what you mean. What DOM structure do you want to end up with? Some before and after examples would be super helpful.
@ssube PLease see improve question above. Thanks. Ill wait for reply
0

Try

$(function() {    
        setInterval(loop, 1000);
        function loop() {
          var n = "0123";
          for(var i=0;i<=3;i++) {
            $(".found").find(".find[id*=ID]").html(n);
          }    
        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="found">
      <div class="find" id="myID"></div>
</div>

 <div class="found">
      <div class="find" id="anID"></div>
</div>

<div class="found">
      <div class="find" id="anID2"></div>
</div>

3 Comments

aS I said. removing .empty(); function will make the code append infinitely. Which is not what I want.
can u please change the .html(n) ti i? so that it prints the numbers? please. I will try to run it
@KeanAllen Tried stacksnippets ? "0123" is appended to .find . No "numbers" are printed ; "0123" is String within html
0

Modified the code as You want it to happen just once Run the original code if you want to keep doing it

    <html>
    <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
      <style>

      </style>
    </head>
    <body>
    <div class="found">
      Hello World
    </div>
    <div class="found">
          <div class="find" id="myID"></div>
    </div>

     <div class="found">
          <div class="find" id="anID">Append here</div>
    </div>

    <div class="found">
          <div class="find" id="anID2"></div>
    </div>
    <script>
    $(document).ready(function(){


    //$('#anID').empty();
for(var i=0;i<=3;i++)
{
    $('<p>'+i+'</p>').appendTo('#anID');

    //$('.found').append(i);
    //$('.found').append("\n"); 

}


});
    </script>

    </body>
    </html>

5 Comments

see Improved question above
@KeanAllen changed as per your requirement
what changed as per requirement?
Nothing changed, you can create as many div tags. Jquery will find appropriate div based on id and start append 0123 to it
Thanks. I already had an answer I just improved your code. Thanks BTW. SEE my answer above
0
  $(function() {    
    setInterval(loop, 1000);
    function loop() {

        $(".found").find(".find[id*=ID]").empty();
      for(var i=0;i<=3;i++)
      {
        $(".found").find(".find[id*=ID]").prepend(i);
      }    
    }
});

3 Comments

Calling .empty() is not necessary. .html() will replace html within .find(".find[id*=ID]") . See stackoverflow.com/a/30222010 . Also , at OP, requirement appeared to be "0123" , not "3210" ?
If .html it Only prints the last value, while .empty() it all prints the value :)
Also , at OP, requirement appeared to be "0123" , not "3210" "0123 only" stackoverflow.com/questions/30221839/… ?

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.