I have 4 divs, each contain a link which opens a new different div below all 4 (it's a section about services), for mobile screens (669px and below) I need the div which the link opens to display underneath the div containing the link instead of below all 4 divs.
I've tried some of the answers on here already such as
function resize(){
if($(window).width() < 670){
$("#service1").insertAfter("#service-box1");
}
}
but I'm sure I'll be doing something wrong when putting it in my code
Here's the simplified html for the divs
<div id="services-container" class="row">
<div class="service" id="service-box1">
<h1>Title</h1>
<a class="learn-more" id="show1" data-href="service1">Learn More</a>
</div>
<div class="service" id="service-box2">
<h1>Title</h1>
<a class="learn-more" id="show2" data-href="service2">Learn More</a>
</div>
<div class="service" id="service-box3">
<h1>Title</h1>
<a class="learn-more" id="show3" data-href="service3">Learn More</a>
</div>
<div class="service" id="service-box4">
<h1>Title</h1>
<a class="learn-more" id="show4" data-href="service4">Learn More</a>
</div>
<div class="service-content" id="service1">
<h1>Service1</h1>
</div>
<div class="service-content" id="service2">
<h1>Service2</h1>
</div>
<div class="service-content" id="service3">
<h1>Service3</h1>
</div>
<div class="service-content" id="service4">
<h1>Service4</h1>
</div>
</div>
Here's the function to open the service-content div for the first div
$('document').ready(function() {
$('#show1').click( function() {
var $div = $('#' + $(this).data('href'));
$('.service-content').not($div).hide();
$div.slideToggle();
});
});
That code opens the service-content divs below all 4 of the service divs. On mobile screens I need service1 to display under service-box1 and above service-box2 and so-on. I don't know whether this means reworking the whole code or if there's a function to do it, any help is appreciated.