Hey there, I have a question about how to use the data-target
attribute to set the display of a div. Check out my HTML code below:
<div class="page page-current" id="home">
PAGE 1
<a href="#" class="next" data-target="#about">Go to about</a>
</div>
<div class="page page-section" id="about">
PAGE 2
<a href="#" class="next" data-target="#portfolio">Go to portfolio</a>
</div>
<div class="page page-section" id="portfolio">
PAGE 3
</div>
In the code above, the divs with the class: page-section
are initially hidden using CSS with: display:none
. I was able to write a generic JavaScript function that determines which div should be visible:
$(".next").click(function(){
var target = $(this).data("target");
$(target).addClass("page-current");
})
The issue now is that, while it adds the class: page-current
to the second div, the first one still has this class as well.
Can anyone give me some advice on how to solve this in a way that ensures only one div at a time can have the class: page-current
?
I've created a JSFIDDLE to demonstrate this problem.