How do you get id from the previous and the next div through js?
-
How do you get id from the previous and the next div through js? And if it's over, it's done.
alert('Закончилось!');
/* PREV */ $(".prev").on("click", function() { $("div.active:not(:first-child)").removeClass("active").prev().addClass("active").click(); }); /* NEXT */ $(".next").on("click", function() { $("div.active:not(:last-child)").removeClass("active").next().addClass("active").click(); });
body { background: #EEE; } div { width: 100px; height: 150px; background: #FFF; color: #555; margin: 10px; float: left; } div.active { background: #555; color: #FFF; } button { float: left; font-size: 12px; margin: 5px; padding: 8px 8px; color: #FFF; background: #555; cursor: pointer; border: 0; } button:hover { background: #444; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="1"> Block 1 </div> <div id="2" class="active"> Block 2 </div> <div id="3"> Block 3 </div> <button class="prev"> Предыдущий id 1 </button> <button class="next"> Следующий id 3 </button>
-
Turn the div's into the general container and rewrite the selters for the
.prev()
and.next()
to look inside this container. Then for the first element. https://api.jquery.com/prev/ return the empty jQuery target. Same for the last one..next()
♪But it's better to know all the id elements in advance, jump, keep them in the mass. Maintain the variable meaning of the current, and press the buttons forward/to change the meaning of this variable, if possible. And rewrite the styles by running through everything, or by changing the former current and new.