Tips for removing a particular slide from bootstrap carousel with JQuery

I'm a beginner when it comes to bootstrap. I have a Carousel with some slides that include a Decline button. When the button is clicked, I want to delete/remove that slide and move on to the next one. Can this be achieved using JQuery? I've been experimenting with something similar in JSFiddle.

var $carousel = $('#myCarousel'),
    $carouselItems = $('.item', $carousel);

$carousel.on('slid.bs.carousel', function (e) {
    $carouselItems.removeClass('prev next');
    var $active = $(e.relatedTarget);
    $active.next().addClass('next');
    $active.prev().addClass('prev');
})

$(document).ready(function(){
    $("button").click(function(){
        $("#div1").remove();
         $("#li1").remove();
        $("#li2").attr("data-slide-to",0);
         $("#li2").attr("data-slide-to",1);
         $("#div2").addClass('active');
    });
});

Any help would be greatly appreciated. Thank you.

Answer №1

When a button is clicked, the script will identify the active slide and remove it from the DOM. It will then set the next .item element as active.

JavaScript:

var $carousel = $('#myCarousel');
$("button").click(function() {
  currentIndex = $('div.active').index();
  var ActiveElement = $carousel.find('.item.active');
  ActiveElement.remove();
  var NextElement = $carousel.find('.item').first();
  NextElement.addClass('active');
});
.content-area {
    position: relative;
    width: 460px;
    margin: 0 auto;
}
.carousel-indicators li {
    border-radius: 50%;
    height: 20px;
    width: 20px;
}
.carousel-indicators .active {
    height: 25px;
    width: 25px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<div class="content-area">
    <div id="myCarousel" class="carousel slide">
        <!-- Indicators -->
        <ol class="carousel-indicators">
            <li data-target="#myCarousel" id="li1" data-slide-to="0" class="active"></li>
            <li data-target="#myCarousel" id="li2" data-slide-to="1"></li>
            <li data-target="#myCarousel" id="li3" data-slide-to="2"></li>
        </ol>
        <!-- Wrapper for slides -->
        <div class="carousel-inner">
            <div class="item active" id="div1">
                <img src="http://placekitten.com/960/600">
            </div>
            <div class="item" id="div2">
                <img src="http://placebear.com/960/600">
            </div>
            <div class="item" id="div3">
                <img src="http://placekitten.com/960/600">
            </div>
        </div>
    </div>
    <!-- Controls --> <a class="left carousel-control" href="#myCarousel" data-slide="prev">
    <span class="icon-prev"></span>
  </a>
 <a class="right carousel-control" href="#myCarousel" data-slide="next">
    <span class="icon-next"></span>
  </a>

</div>
<div>
    <button>Remove div element</button>
</div>

Note: Remember to include code for updating Carousel Indicators when removing an element from the DOM.

Answer №2

This piece of coding brilliance really saved the day for me. I attempted to manipulate the carousel on my own but ended up freezing it. Your code solved my problem perfectly. One small observation though, there seems to be a redundant line of code present: currentIndex = $('div.active').index(); It looks like the currentIndex variable from this line is not being used anywhere in the code.

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

Could anyone provide recommendations on how to globally declare jQuery in a Vue-CLI 3 project?

I've come across numerous inquiries on various platforms about this issue, but I have yet to find a solution. To provide some background, I constructed a single-page application website using Vue by initiating a project with an older command. Althoug ...

Struggling to format boxes or display images on your WordPress website?

When working on this website, I encountered an issue with adding links to Google+ and Facebook. Despite my efforts to style the div boxes using CSS, I was unsuccessful. I also attempted to insert images using an img tag, but the pictures did not load even ...

Collecting values using AJAX in Codeigniter from an input file

I have been exploring examples of AJAX code, and I noticed that when using form-data in AJAX, we need to serialize each element individually. While normal data serialization works fine, I encountered an issue when trying to pass the value of an input file ...

Why ASP .NET MVC's AJAX BeginForm InsertionMode.Replace feature is causing issues

When I input anything into the text field, I can see the result displayed in the desired DIV. However, upon inspecting the page's source code, I cannot locate the replacement element. For instance, if I input 'aaaaaaaaaaaaaaaa', then click ...

"Initiate an Ajax call in Full Calendar before an event is displayed on the calendar

I need guidance on how to integrate ajax calls with the Full Calendar documentation. Specifically, I want to make an ajax call to a WordPress database before each event is rendered on the calendar. The response from the call will determine the color of the ...

What could be causing the delay in populating 10,000 records in the jquery DataTable()?

I've been working with the Jquery DataTable() library and have noticed that it takes a long time to populate if the data array exceeds 10,000 entries. var newTable = ''; var ref = firebase.database().ref("Students/"); ref.on('child ...

Having trouble getting the swiping feature to function on jQuery mobile

Hey there, I'm new to this so please bear with me if I'm not getting everything right with posting... I've been trying to figure out how to swipe left and right for jquery mobile by visiting a few pages. I found this page for my script - - ...

When combining <a> with the class line-through, the line does not appear

I am currently utilizing a class to replace the deprecated strike in HTML: .entfall { text-decoration: line-through; } However, when using Firefox 38.x and the following code: <span class="entfall"><a href="some link">text</a></span ...

Performing PHP MySQL table database insertion utilizing the $_GET approach using Codeigniter-3 and Dropzone-4.1 plugin

Currently working with CodeIgniter 3 and running into an issue with "id_case" showing as undefined index. When I click a link in index.php: <?php echo "<td><a href='/ci_dropzone/?id_case=".$row['id_case']."'>Upload File ...

Looking to add a close button to the iFrame, but having trouble getting it to function when clicked

In my project built with Meteor, I have integrated an iframe to display specific content to the user. The iframe is functioning as expected, but I am looking to add a close button that will effectively unload the iframe when clicked. I attempted to imple ...

The contents within the div are failing to scroll upwards

Looking to create a temporary reading tool to enhance my English skills for the IELTS exam. Currently, I am working on incorporating 1-40 answer fields on the right-hand side of the screen. https://i.sstatic.net/EwoMM.png I'm facing issues with scro ...

Ajax activates a slider

One element I have is a menu div, along with a content div Initially, when the slider (which is a jquery plugin) is placed in the index.php, everything appears to be working well, as shown in the image below However, my objective is to have each page&apo ...

What are the steps for utilizing fnServerData function in jQuery?

Would someone be able to demonstrate the implementation of fnServerData? $(document).ready( function() { $('#example').dataTable( { "bProcessing": true, "bServerSide": true, "sAjaxSource": "xhr.php", "fnServerData": function ( ...

Setting MenuItem to the correct width in asp.net simplified

I have a 1000px wide container, within it there is a menu control with 5 items (links). Each item has a width of 200px in the CSS file to make use of the entire length of the line. .level1 { border-width: 0px; margin: 0px; padding: 0px; background ...

Modify CSS using Javascript by targeting the class of ".modalDialog > div {}"

I am currently working on a project that involves creating a Modal Layer using CSS. I have successfully implemented the modal layer, but now I need to figure out how to change the width from the default 400px to something else when I open it with Javascrip ...

Having trouble with the repeat-item animation feature not functioning correctly

Take a look at this Plunker, where the animation for adding an item only seems to work properly after an item has been deleted from the list. Any suggestions on how to resolve this issue? Here is the CSS code: .repeat-item.ng-enter, .repeat-item.ng-leave ...

Jquery Error: Unable to split object

Why am I getting an error saying "Uncaught TypeError: Object # has no method 'split'" when trying to run this page by clicking on the dropdown and triggering a change event that sends an AJAX request? <html xmlns="http://www.w3.org/1999/xhtm ...

I am interested in modifying the color of the tick marks on the input[range] element

Seeking Slider Customization Help I am eager to learn how to create and personalize a slider using HTML. Although many resources offer guidance on customizing the slider itself, I have yet to come across any information on customizing the scale. For insta ...

What is the process for creating an HTML file that can automatically save?

I am looking to develop a unique HTML document where users can enter text in text fields, save the file by clicking a button, and ensure that their changes are not lost. It's like what wysiwyg does for HTML documents, but I need it to be integrated di ...

Handling AJAX requests in ASP.NET for efficient communication

I'm in the process of setting up ajax communication between a JavaScript frontend and an ASP.NET backend. While researching, I came across this sample code on w3schools: function loadDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatecha ...