Swap out the image backdrop by utilizing the forward and backward buttons

I am currently working on developing a Character Selection feature for Airconsole. I had the idea of implementing this using a Jquery method similar to a Gallery. In order to achieve this, I require a previous button, a next button, and the character display section. However, I seem to be encountering an issue that I can't quite resolve, probably because I am not very familiar with Javascript.

var speed = 100;

$(".prev").click(function() {
  var now = $(this).parent().next("ul.char_display").children(":visible"),
    last = $(this).parent().next("ul.char_display").children(":last"),
    prev = now.prev();
  prev = prev.index() == -1 ? last : prev;
  now.fadeOut(speed, function() {
    prev.fadeIn(speed);
  });
});

$(".next").click(function() {
  var now = $(this).parent().next("ul.char_display").children(':visible'),
    first = $(this).parent().next("ul.char_display").children(':first'),
    next = now.next();
  next = next.index() == -1 ? first : next;
  now.fadeOut(speed, function() {
    next.fadeIn(speed);
  });
});

$(".char_display li").click(function() {
  var first = $(this).parent().children(':first'),
    next = $(this).next();
  next = next.index() == -1 ? first : next;
  $(this).fadeOut(speed, function() {
    next.fadeIn(speed);
  });
});
.prev {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

.next {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

.char_display li {
  display: none;
  list-style: none;
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}

.char_display li:first-child {
  display: block;
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}

.char_display {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}

.char {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}

#char1 {
  background: blue;
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}

#char2 {
  background: red;
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="characterScreen_characterBlock">
  <div id="characterScreen_leftArrow" class="characterScreen_left" ontouchend="otherChar(true);" ontouchend="otherChar(true);">
    <div class="prev"></div>
  </div>
  <div id="characterScreen_characterDisplay" class="characterScreen_center">
    <ul class="char_display">
      <li>
        <div class="char" id="char1"></div>
      </li>
      <li>
        <div class="char" id="char2"></div>
      </li>
    </ul>
  </div>
  <div id="characterScreen_rightArrow" class="characterScreen_right" ontouchend="otherChar(false);" ontouchend="otherChar(false);">
    <div class="next"></div>
  </div>
</div>

Answer №1

After reviewing some of your code, it appears that

$(this).parent().next("ul.char_display").children("...")

may not be achieving the desired outcome, particularly as it consistently triggers an error when pressing the next button.

In this context, $.next() retrieves the subsequent DOM element. However, in the case of characterScreen_rightArrow, there is no subsequent element present (as shown in this snippet).

The functionality you are likely seeking can be achieved with $.siblings(). In my opinion, a more efficient approach would involve targeting

#characterScreen_characterDisplay ul
directly instead of locating it relatively.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can you implement a resource response approach in Express.js using an API?

As a newcomer in my journey with expressjs, I'm currently exploring its functionalities. In my controller, I've structured the response as follows: .... res.json({ 'data': { 'user': { 'id': us ...

Accessing props in setup function in Vue 3

I am encountering issues when trying to access the props value (an array) in my composition API setup. The component I have is called DropDown, and I am passing it an array of objects. Here's what I need to achieve: export default { emits: ['up ...

Struggling to contain the image inside my div element

I'm having trouble with keeping the image inside my div element as it keeps stretching outside of it. I've tried researching for a solution but haven't been successful in finding one. Any help would be greatly appreciated! Here is the HTML/ ...

Encountering persistent issues while attempting to integrate socket.io into my Node.js application with Express

Recently, I've been exploring ways to create a textbox that allows multiple users to type simultaneously. Below is the HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> ...

Click to make the arrow come to life through animation!

I am brand new to coding and this is my inaugural code snippet: .container_menu { position: relative; top: 0px; left: 0px; width: 25px; height: 25px; } .container_menu .line-1 { background-color: black; width: 59%; height: 2px; positio ...

Using inline SVG in a Vite production build

When utilizing Vite and Vue, my goal is to compile a single JavaScript file without creating an assets directory that includes SVG and GIF files during the build process. I want to achieve this without manually inserting the SVG code in a Vue JS file as a ...

Tips for modifying the value and refreshing the array

I retrieve data from $scope.roleinfo = {"QA":[{"White Box Testing":0},{"Black Box Testing":0}],"Development":[{"Server Side":0},{"UI":0},{"Back end":0}]}; Then, I present these values in a table. Now, I need to update the maxcount and create an array w ...

Combining Django Bootstrap input-group-addon with Model Form for seamless field integration

Currently, I am displaying my Django form in the following manner: <form action="/student/" method="post"> {% csrf_token %} {% bootstrap_form form %} <input type="submit" value="Launch" class="btn btn-primary"/> </form> I ha ...

HTML: Launch Frameset in a Separate Tab or Window

I have encountered an issue with using FRAMESET. Situation: Within my frameset, I have a menu with 5 links. When I click on the home page, everything looks fine. However, when I open it in a new tab or window, the design is not displayed. Is there a way ...

Incorporate innerHTML by appending instead of overwriting

Just a quick question, can we actually add content to a <div id="whatEverId">hello one</div> by utilizing: document.getElementById("whatEverId").innerHTML += " hello two"; Is there a way to INSERT content into the div without replacing it??? ...

The sharing feature is not functioning properly within content loaded via AJAX

My website has a feature that dynamically pulls in content using AJAX requests and JQuery. After the content is loaded, I use colorbox's onComplete function to load any necessary scripts. One of the things I'm trying to load is a ShareThis button ...

Attempting to insert a line break tag within the text using React

Having trouble inserting line breaks in text using React. Can anyone guide me on how to achieve this? I've attempted adding the br tag, but it is displaying as in the browser. Here's a snippet of my code. Appreciate any help or advice. let sp ...

Having trouble with changing images or background images in JavaScript?

Encountering issues with dynamic elements. When I click a button, the following JS function is executed. It displays a stylish CSS alert upon postback and then runs the function. function scrollToTop(sender, args) { window.scrollTo(0, 0); document. ...

Warnings from Webpack may appear while running the Next.js development server

Encountering these warnings when running npm dev: <w> [webpack.cache.PackFileCacheStrategy] Restoring pack from /Users/pdeva/code/monorepo/web/app/.next/cache/webpack/client-development.pack failed: TypeError: Cannot read properties of undefined (rea ...

"Troubleshooting: Why is my AngularJS ng-click not triggering the function

My custom directive fetches a navigation block from a webservice call. I am attempting to click on a navigation link that has an "ng-click" attribute set, which should call the specified function. However, the function is not being executed. Below is my r ...

aws-lambda Module Not Found

I am encountering an issue in the aws-lambda console every time I try to upload code from a zip file. Oddly, other zip files seem to work fine. The .js file within the problematic zip is named "CreateThumbnail.js" and I have confirmed that the handler is ...

The functionality of CSS isn't up to snuff on Mozilla Firefox

I am having an issue with CSS styling on a <div> element in my PHP page. The CSS style is working perfectly in Google Chrome, but when I test my code in Firefox, the styling is not being applied. .due-money { background-color: #09C; color: whi ...

Problem with Safari: File downloaded with name "Unknown" due to Javascript issue

After successfully converting my data to text/csv, I can easily download the file in Chrome. However, when attempting to do so in Safari on an iPad or Mac, it opens a tab with the name "unknown" or "Untitled". The code snippet I am using for this is as fol ...

Tips for resolving vertical alignment styling for images of varying sizes within Vue.js transition-group?

I have created an image slider example, but I'm facing a styling issue when using images of different dimensions. The element leaving the array is positioned absolutely for smooth transitions, but this causes the image to move up. I am trying to vert ...

Changing the appearance of a website by switching CSS stylesheets according to the size of the browser

Trying to build two versions of my website: one for mobile devices or small browsing windows, and another for large browser windows. Looking to link HTML to different style sheets based on browser size. Current attempt in code only addresses total screen ...