Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 76195

Move divs with specific class to top

$
0
0

I want to add class "active" on button click to divs that have "sort" class and then move divs with "active" class to top position in container. So the result will be in this order: 2,4,1,3

$('button').click(function(){
  $('.container div').each(function() {
  if($('.container div').hasClass('sort')) {
    $(this).addClass('active').prependTo('.container'); }
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class="container"><div>1</div><div class="sort">2</div><div>3</div><div class="sort">4</div> </div><button>click</button>    

Viewing all articles
Browse latest Browse all 76195

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>