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.
2
Answers
a) You need to add the base jQuery library before any other jQuery library file, then only your jQuery code will run. So swap the position of 2 js files added.
b) Instead of adding and removing, toggle the disabled property on the buttons
c) You need to modify
funcUpdate()
like below:Working snippet:
You were missing a start tag
<main>
so I added one.You can simplify the script to just account for however many images you have, trigger it on the first one to get started and just pass the function the current value.
Use
.prop()
for the disabled.