Exploring the functionalities of a personalized jQuery slider's next button

I created a container div with three inner divs, each having a class of "content" and one of them having a class of "current". My goal was to display only one ".content" at a time and have a "next" button that hides the current div, removes the "current" class, and adds it to the next one. Everything seemed to work well until I reached the end of the divs. I wanted it to loop back to the first one but the code didn't achieve that. The first one shows up fine, but the next button stops functioning after:

Here is the JavaScript code snippet:

var $ = jQuery

$( document ).ready( function() {


  $('#next').click(function(){

  var item = $('.content.current');
  item.removeClass('current');
  item.slideUp();
  item.next().addClass('current');
  if(item.next().length == 0) {

    $('.content').first().slideDown().addClass('current').end();

  }
  })
})

And here is the structure of the HTML:

<div id="holder">
        <a class="btn btn-warning next" id="next">Next</a>
        <div class="wrap">

        <div class="content current">

        <div id="header"><h3>Basic</h3></div>

        <ul>
          <li>Unlimited Bandwidth</li>
          <li>Unlimited Diskspace</li>
          <li>10 Email Accounts</li>          
          <li>24/7 Support!</li>
          <li>$4/month</li>
          <li><a class="btn btn-success">Order Now!</a></li>
        </ul>

        </div>
        <!-- .content -->

        <div class="content">

        <div id="header"><h3>Professional</h3></div>

        <ul>
          <li>Unlimited Bandwidth</li>
          <li>Unlimited Diskspace</li>
          <li>10 Email Accounts</li>          
          <li>24/7 Support!</li>
          <li><a class="btn btn-success">Order Now!</a></li>
        </ul>

        </div>
        <!-- .content -->

        <div class="content">

        <div id="header"><h3>St. Joseph</h3></div>

        <ul>
          <li>Unlimited Bandwidth</li>
          <li>Unlimited Diskspace</li>
          <li>10 Email Accounts</li>          
          <li>24/7 Support!</li>
          <li><a class="btn btn-success">Order Now!</a></li>
        </ul>

        </div>
        <!-- .content -->



        </div>
        <!-- .wrap -->

      </div>
      <!-- holder -->

Answer №1

The function

$('.content').first().slideDown().addClass('current').end();
reveals the first element and applies the current class to it. To achieve this, modify your script as follows:

var $ = jQuery
$( document ).ready( function() {
  $('#next').click(function(){
  var item = $('.content.current');
  item.removeClass('current');
  item.slideUp();
  item.next().addClass('current');
  if(item.next().length == 0) {
    $('.content').slideDown();
    $('.content').first().addClass('current');
  }
  });
});

This revised version should work correctly. You can also view a demo on JSFiddle. I hope this solution helps you.

Answer №2

var $ = jQuery

$( document ).ready( function() {


    var i = 1;

    $('#next').click(function(){

        ++i;

        $('.content.current').removeClass("current").slideUp();
        $(".content:nth-child("+i+")").addClass("current").slideDown();

        if(i == 3) i=0;
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="holder">
        <a class="btn btn-warning next" id="next">next</a>
    <div class="wrap">

        <div class="content current">

        <div id="header"><h3>Basic</h3></div>

        <ul>
          <li>Unlimited Bandwidth</li>
          <li>Unlimited Diskspace</li>
          <li>10 Email Accounts</li>          
          <li>24/7 Support!</li>
          <li>$4/month</li>
          <li><a class="btn btn-success">Order Now!</a></li>
        </ul>

        </div>
        <!-- .content -->

        <div class="content">

        <div id="header"><h3>Professional</h3></div>

        <ul>
          <li>Unlimited Bandwidth</li>
          <li>Unlimited Diskspace</li>
          <li>10 Email Accounts</li>          
          <li>24/7 Support!</li>
          <li><a class="btn btn-success">Order Now!</a></li>
        </ul>

        </div>
        <!-- .content -->

        <div class="content">

        <div id="header"><h3>St. Joseph</h3></div>

        <ul>
          <li>Unlimited Bandwidth</li>
          <li>Unlimited Diskspace</li>
          <li>10 Email Accounts</li>          
          <li>24/7 Support!</li>
          <li><a class="btn btn-success">Order Now!</a></li>
        </ul>

        </div>
        <!-- .content -->

    </div>
        <!-- .wrap -->

</div>
      <!-- holder -->

It's quite simple when using JS :

$( document ).ready( function() {

    var i = 1;

    $('#next').click(function(){

        ++i;

        $('.content.current').removeClass("current").slideUp();
        $(".content:nth-child("+i+")").addClass("current");

        if(i == 3) i=0;
    })
})

Alternatively, you can utilize BOOTSTRAP which is likely being used.

You may refer to it here: Collapse Bootstrap

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

Mastering the Art of Utilizing Box Component Spacing Props

Easy margin and spacing adjustments are possible with the help of Material-UI. They provide shortcuts that eliminate the need to individually style components with CSS. In the code snippet below, it's important to note that specifying the measuremen ...

Compatibility of Typescript with local storage

Can Typescript interact with local storage in any way? I have been setting items using the following code: localStorage.setItem('someString', JSON.stringify(MyTypescriptObject)); This method stores the object as a plain string. For example: ...

How to apply jQuery addClass to only the following target element and not all elements on the entire webpage

Currently, I am using jQuery to create an animation effect on a hidden element (which is an image description) so that it becomes visible when hovering over an image. Although the code snippet I found does work, it has a downside - all the hidden image de ...

Excessive CPU usage caused by a patch in jQuery dealing with regular expressions

Working on a project developed by an unknown individual has presented some challenges. Without any means of contact with this person, I noticed that the browser's CPU consumption spikes significantly upon loading the page. Upon further investigation, ...

Is there an easy method to verify if the local storage is devoid of any data?

Query is named aptly; seeking to verify in a conditional statement. ...

Checking for an empty value with javascript: A step-by-step guide

Below is an HTML code snippet for checking for empty or null values in a text field: function myFormValidation() { alert("Hello"); var name = document.getElementById("name").value; alert(name); if (name == null || name == "") { document.ge ...

What could be causing the component not to display the accurate data in ReactJS?

Currently, I am working on rendering an array of values along with two buttons - one for adding and the other for removing elements. Everything seems to be working fine, except for the removal functionality. When I click on the Remove button for a specific ...

Utilize jQuery to store unload events for future implementation

One idea I'm considering is saving an unload event to be executed at a later time. Here's an example: // on unload do nothing but safe the event jQuery(window).unload(function() { // TODO safe the unload event here somehow ...

JQuery is detecting an outdated file version

Currently, I am in the process of working on a project for my Raspberry Pi that focuses on gathering network speed information and then logging it in a local webserver. The script itself is functioning perfectly fine, however, I have encountered an issue w ...

The single answer input field is not displaying as a readonly text input

Here is a jsfiddle link Please pay attention to Question 2 in the jsfiddle table, as it contains only one answer. In the jquery function, I have attempted to make the text input under the Marks Per Answer column readonly if a question has only one answer ...

Guidance on uploading a file with AJAX in PHP following the display of a Sweet Alert

I am currently facing an issue with inserting a file input (images) into my database. When I try to insert it, the value returned is empty. Below is the script I am using: <script> $("#insertform").on('submit',(function(e) { ...

Prevent double tap selection

At times, I enable the user to click on the <li> or <span class="icon"> elements, but if they happen to click twice, it causes a bit of chaos and spreads the blue selection all over :/ To address this issue, I have come up with two solutions: ...

Is it possible to use the Splinter module in Python to choose a specific item from a dropdown menu on a webpage?

Is there a way to use the splinter module in Python to select a specific element from a dropdown list on a webpage? Here is an example of the HTML code: <select id="xyz"> <optgroup label="Group1"> <option value="1">pick1</op ...

Perform the action when the button is clicked

I'm struggling with finding a solution to this problem. I have an input field and a submit button. My goal is to update the value of a variable to match the value in the input field when the submit button is clicked. Additionally, I need to execute a ...

JavaScript is claiming that my class method is invalid, despite the fact that it is obviously a valid function

Implementing a genetic algorithm using node has been my latest challenge. After browsing through the existing questions on this topic, I couldn't find anything relevant to my issue. The problem arises when I attempt to call the determine_fitness metho ...

Printing content from a web page using window.print() function in a form using javascript and angular

Within my HTML code lies a form, complete with a variety of fields: <div class="card-body" id="print"> <form [formGroup]="form"> <div class="text-muted" *ngFor=" ...

Is there a way to simulate the Bun global object using Jest?

Currently, my tech stack includes Bun, Typescript (TS), and Jest. As I work on writing my tests, I encounter the need to mock Bun.file functionality. Here is a snippet from my tsconfig.json: { "compilerOptions": { "lib": ["ESNext"], " ...

Determine the number of elements located inside a designated slot

Take a look at this Vue component code: <template> <!-- Carousel --> <div class="carousel-container"> <div ref="carousel" class="carousel> <slot></slot> </div> </div&g ...

Search for text in multiple tables using jQuery and automatically trigger a button click event when the text is found

I am attempting to query certain tables and click a button within a cell of a specific table. Below is the code I am currently using: links[1].click(); iimPlayCode('WAIT SECONDS = 2') var compTabs = window.content.document.getElementById(' ...

Eliminating unique phrases from text fields or content sections with the help of jQuery or JavaScript

I'm currently working on a PHP website and have been tasked with the responsibility of removing certain special strings such as phone numbers, email addresses, Facebook addresses, etc. from a textarea that users input data into. My goal is to be able ...