I've seen this question asked and answered before but after trying several proposed solutions I still haven't had any luck.
I have four buttons ( respectively with IDs "frst, prev, next, last,") None of them do anything when clicked. Currently the part of my code that isn't working looks like so:
const index = ['imgs/1.png', 'imgs/2.png', 'imgs/3.png', 'imgs/4.png', 'imgs/5.png'];
let current = index.length;
$(document).ready(function(){
$(document).on('click', '#frst', function(){
current = 1;
funcUpdate();
});
$(document).on('click', '#prev', function(){
current--;
funcUpdate();
});
$(document).on('click', '#next', function(){
current++;
funcUpdate();
});
$(document).on('click', '#last', function(){
current = index.length;
funcUpdate();
});
});
function funcUpdate() {
$('#img').attr('src', index[current - 1]);;
$('button').removeAttr('disabled');
if (current == 1) {
$('.back').attr('disabled', '1');
}
else if (current == index.length) {
$('.frwd').attr('disabled', '1');
}
}
My html looks like this:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script src="imgs.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
<body>
<div>
<img id="img" src="imgs/5.png"/>
<div class="center">
<button type="button" class="back" id="frst">ππ</button>
<button type="button" class="back" id="prev">π</button>
<button type="button" class="frwd" id="next">π</button>
<button type="button" class="frwd" id="last">ππ</button>
</div>
</div>
</main>
</body>
</html>
funcUpdate is meant to switch out the displayed image and disable buttons so you can't go outside the values currently in the array. When I don't use jQuery, like below, it works as intended.
<button class="back" onclick="funcFrst()">first</button>
<button class="back" onclick="funcPrev()">previous</button>
<button class="back" onclick="funcNext()">next</button>
<button class="back" onclick="funcLast()">last</button>
With code in imgs.js like so:
function funcFrst() {
current = 1;
funcUpdate();
}
function funcPrev() {
current--;
funcUpdate();
}
function funcNext() {
current++;
funcUpdate();
}
function funcLast() {
current = index.length;
funcUpdate();
}
In both cases, the js is in imgs.js.
I've also tried $('#ID').click
and $('#ID').on('click', function(){});
, each in and outside of $(document).ready
with no luck. I've also seen it suggested to put the script below the buttons that call it, which also didn't work.
I bet it's probably some really obvious error I've overlooked but I figured I'd ask anyway.
$(document).ready(function(){ $(document).on('click', '#frst', function(){
should be simply$(function(){ $('#frst').on('click', function(){
You want to hook the event handler to the element not the document.document
makes it traverse more of the DOM. Especially here where it is attaching multiple event handlers for no reason really. Better to attach to elements with an ID since that is a fast selector OR to the container if you are delegating so the traversal is smaller (I put that in my answer since that seems like it might be less code overall to do so).