4

The following codes makes div appear sequentially.

$(document).ready(function() {
    $('.word1, .word2, .word3').each(function(fadeIn) {
      $(this).delay(fadeIn * 500).fadeIn(1000);
    });
  });
  .chat {
    display: none;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

  <div class="chat word1">Word 1</div>
  <div class="chat word2">Word 2</div>
  <div class="chat word3">Word 3</div>
  
  
  <div id="" class="">Word 4</div>
</body>

What I want to do is, I don't want it to appear in a sequence. I can do it by simply replacing elements in an html, for example I can do:

  <div class="chat word2">Word 2</div>
  <div class="chat word1">Word 1</div>
  <div class="chat word3">Word 3</div>

However, I don't want to change anything on the html elements. I want to do it using javascript. At first, I thought javascript selector works like an array and I can replace

$('.word1, .word2, .word3') with $('.word2, .word1, .word3')

but it does not seems to work that way.

Is there a way to do this with Javascript?

2
  • not sure what you want to achieve, but looks like you want the html show not in the order it was written. you could use css flex order (developer.mozilla.org/en-US/docs/Web/CSS/order) or jquery sortable (jqueryui.com/sortable) Commented Sep 28, 2018 at 4:06
  • That's right, not in order. I'll give it a try tomorrow, thanks! Commented Sep 28, 2018 at 4:25

5 Answers 5

1

Here be a solution if you do not want to change your HTML(and incase css also):

  1. Keep the shuffle Position in array.

  2. Iterate all div having class chat.

  3. Put the DOM element in new array based on shuffle Position.

  4. Iterate all element of new array and append in body.

$(document).ready(function() {
 var shufflePosition=[1,0,2];//Keep the shufflePosition in array
 var result=[];

 //Iterate all div having class chat
 $('.chat').get().forEach(function(entry, index, array) {
     result[index]=array[shufflePosition[index]];
 });

 for (var i = result.length-1; i >= 0; i--) {
    $( "body" ).last().prepend(result[i]);
    //$(result[i]).show();
    $(result[i]).delay(i*500).fadeIn(1000);
 }
});
.chat {
    display: none;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

  <div class="chat word1">Word 1</div>
  <div class="chat word2">Word 2</div>
  <div class="chat word3">Word 3</div>
  <div id="" class="">Word 4</div>
</body>

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

3 Comments

Avoid $.remove in this case. It's a complicated method that goes deep inside the element to remove all data and events attached to it, instead in such a case, you might prefer $.detach. But in this exact case, $.prepend is just enough, ParentNode.appendChild method takes care of removing the node if in DOM.
Hi NullPointer, this is what I need, but how do you apply fadetoggle on it?
Oh, it's all good now, I was able to figure it out. Thank you! :)
0

You use a query selector:

var word1 = document.querySelectorAll(".chat", ".word1")[0];

This selects the first element with both classes .chat and .word1.

2 Comments

No problem whatsoever!
What is this second param?
0

how about this

$(document).ready(function() {
    $('.chat').delay(500).fadeIn(1000);
  });
  .chat {
    display: none;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

  <div class="chat word1">Word 1</div>
  <div class="chat word2">Word 2</div>
  <div class="chat word3">Word 3</div>
  
  
  <div id="" class="">Word 4</div>
</body>

3 Comments

I dont understand what exactly you want. but if you want show word1,word2,word3 at once my answer will works
Hi, i want to be able to show those divs not order. Currently it shows in order. I can do it by changing divs but i want to do it using javascript.
OK. you want div shows one by one and it works well. your problem is that you want mix div orders EX) word1 word3 word2 OR word2 word1 word3 is it right?
0

I think the simplest solution, to not care about the order of the Divs, is:

$('.chat').each(function(fadeIn) {
    $(this).delay(fadeIn * 500).fadeIn(1000);
});

You should define a common class for these randomly ordered elements, or wrap them with a parent element, eg:

$('.randomlysortedelements div').each(function(fadeIn) {
    $(this).delay(fadeIn * 500).fadeIn(1000);
});

<body>
  <div class="randomlysortedelements">
    <div class="chat word3">Word 3</div>
    <div class="chat word1">Word 1</div>
    <div class="chat word2">Word 2</div>
  </div>
  <div id="" class="">Word 4</div>
</body>

1 Comment

@Tony-OP already mentioned that he don't want to change the HTML.so in this case you solution will work?
0

You can shuffle your array length and prependTo to word4 something like this:

$(document).ready(function() {

 elements =  $('.word1, .word2, .word3');
 let arrayData = [];
 for(i=0;i<elements.length;i++){
 arrayData.push(i);
 }
 
 shuffleArray(arrayData);
 for(i=0;i<arrayData.length;i++){ 
   $("body:last").prepend(elements.eq(arrayData[i]));
   elements.eq(arrayData[i]).show()
 }
    
    
    function shuffleArray(array) {
      for (var i = array.length - 1; i > 0; i--) {
          var j = Math.floor(Math.random() * (i + 1));
          var temp = array[i];
          array[i] = array[j];
          array[j] = temp;
      }
    }
  });
.chat {
    display: none;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

  <div class="chat word1">Word 1</div>
  <div class="chat word2">Word 2</div>
  <div class="chat word3">Word 3</div>
  
  
  <div id="" class="word4">Word 4</div>
</body>

4 Comments

-OP already mentioned that he don't want to change the HTML.so in this case you solution will work?
OP said he don't wants to change in the HTML of 1,2,3 4 is not stated anywhere.
>>However, I don't want to change anything on the html elements. I want to do it using javascript.
@NullPointer will you append all the options at the end of the body? does that makes sense?

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.