I'm trying to check to see if an id was clicked. If it is do something, otherwise do something else. The else part is working but the click part doesn't seem to be.
$(document).ready(function() {
$("#rotators").click(function() {
$(this).data('clicked', true);
});
if ($('#rotators').data('clicked')) {
$("#rotators").css({
"overflow": "auto"
});
$("#rotators").css({
"height": "auto"
});
} else {
setInterval(function() {
if ($("#rotatelist > a:last").is(":visible")) {
$("#rotatelist a:last").after($("#rotatelist a:first"));
$("#rotators").css({
"overflow": "hidden"
});
$("#rotators").css({
"height": "30px"
});
}
}, 500);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="rotators">
<span id="rotatelist"><a href="#">solutions</a><a href="#">film</a><a href="#">seo</a><a href="#">pr</a><a href="#">web development</a><a href="#">marketing</a></span>
</div>
The fiddle for it is here. It doesn't throw an error but the click check just isn't working.
https://jsfiddle.net/cetx8vaL/
EDIT: Per the (excellent) feedback I will explain my goal. I want each to "rotate" one at a time, however, if the #rotators is clicked I want it to stop rotating and show all of the elements as a vertical list.
if ($('#rotators').data('clicked'))will always be false because you only check it once (on page load).$('#rotators').data('clicked')in your interval function...data('clicked')when the page loads. It would make far more sense to put all the logic which executes within thatifstatement in theclickhandler, as then you know the element has actually been clicked.if/elseparts one time - on page load