Home > Net >  Moving div with jQuery under parent's sibling
Moving div with jQuery under parent's sibling

Time:05-11

I'd like to move .tiletitle between .date and .department. I tried using e.g. $(".tiletitle").appendTo($(".oneline")) but there are multiple .oneline elements on the page. How do I target the parent's sibling and put it between .date and .department?`

<div >
<div>
<div >Title</div>
</div>
<div >
<div >01 May 2022</div>
<div >Cleaning</div>
<div >New York, NY</div>
<div >USA</div>
</div>
</div>

CodePudding user response:

You could do it like this:

$('.sub-section .date').after(function() {
  return $(this).closest('.sub-section').find('.tiletitle')
})

This should also ensure that it works inside you have multiple sub-sections

Demo

$('.sub-section .date').after(function() {
  return $(this).closest('.sub-section').find('.tiletitle')
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div >
  <div>
    <div >Title</div>
  </div>
  <div >
    <div >01 May 2022</div>
    <div >Cleaning</div>
    <div >New York, NY</div>
    <div >USA</div>
  </div>
</div>

  • Related