0
How do I navigate between Divs using jQuery?
This DIV will be exeibida on the mobile phone or tablet on screen, IE the entire viewfinder will appear 1 DIV at a time. When clicking the button, go to the corresponding DIV.
Example: If I’m in tela1 and click on the button, I’ll go to the tela2
$(document).ready(function() {
$('#tela2, #tela3, #tela4').hide();
$(".btn").click(function(e) {
botao = e.target.id;
//alert(botao);
if (botao == "btn-1") {
$('#tela2').show();
$('#tela1, #tela3, #tela4').hide();
} else if (botao == "btn-2") {
$('#tela3').show();
$('#tela1, #tela2, #tela4').hide();
} else if (botao == "btn-3") {
$('#tela4').show();
$('#tela1, #tela2, #tela3').hide();
} else if (botao == "btn-4") {
$('#tela1').show();
$('#tela4, #tela2, #tela3').hide();
}
});
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<div class="row">
<div id="tela1">
<h1>Tela 1</h1>
<a id="btn-1" class="waves-effect waves-light btn">Ir para tela 2</a>
</div>
<div id="tela2">
<h1>Tela 2</h1>
<a id="btn-2" class="waves-effect waves-light btn">Ir para tela 3</a>
</div>
<div id="tela3">
<h1>Tela 3</h1>
<a id="btn-3" class="waves-effect waves-light btn">Ir para tela 4</a>
</div>
<div id="tela4">
<h1>Tela 4</h1>
<a id="btn-4" class="waves-effect waves-light btn">Voltar para a tela 1</a>
</div>
</div>
Thank you.
Imagine a div per fez on the screen. When you click the button, go to the next div. That’s it. This was the easiest way I could find, but I believe there’s a more correct way to do this.
– Tiago
but your example is working
– HudsonPH
As @Hudsonph said in the comment above, your code works, another way I see of doing what you want would be by using the method
load
jQuery, but basically it would be the same thing, call a div or an html page and hide the other.– LeAndrade