Reputation: 331
https://jsfiddle.net/z3q2wtLf/29/embedded/result/
In the attached jsfiddle link above .cd-hero has 3 slides that are activated by click. I'm trying to add a stop event to the jQuery only on the third slide ("thank you") so that the user stops on that slide (doesn't slide right or go back to the previous slides). I am new to jQuery any help on how to achieve this is much appreciated!
I have tried applying the below without success:
$('.cd-btn').on('click', function(event) {
event.preventDefault();
if($(this).hasClass('.thanks')){
event.stopPropagation();
} else {
var activePosition = $('.cd-hero-slider .selected').index();
var selectedPosition = activePosition + 1;
nextSlide($('.cd-hero-slider'), $(''), selectedPosition);
updateNavigationMarker(selectedPosition + 1);
}
});
Upvotes: 0
Views: 124
Reputation: 662
Dee i just updated your jsFiddle, adding a simple condition solution, if count of items "n" is equal to the length don't fire the events.
function nextSlide(container, pagination, n) {
if(n != container.children('li').length){
var visibleSlide = container.find('.selected'),
navigationDot = pagination.find('.selected');
visibleSlide.removeClass('selected from-left from-right').addClass('is-moving').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function() {
visibleSlide.removeClass('is-moving');
});
container.children('li').eq(n).addClass('selected from-right').prevAll().addClass('move-left');
navigationDot.removeClass('selected')
pagination.find('li').eq(n).addClass('selected');
checkVideo(visibleSlide, container, n);
}else{ //Thank You events }
}
https://jsfiddle.net/z3q2wtLf/35/
Upvotes: 0
Reputation: 5156
Try this:
<a href="" class="cd-btn" id="BtnThankyou">Thank you</a>
$('.cd-btn').on('click', function(event) {
if($(this)[0].id == "BtnThankyou"){
return false;
}
event.preventDefault();
var activePosition = $('.cd-hero-slider .selected').index();
var selectedPosition = activePosition + 1;
nextSlide($('.cd-hero-slider'), $(''), selectedPosition);
updateNavigationMarker(selectedPosition + 1);
});
Upvotes: 1
Reputation: 8695
You can add one more class to the last .cd-btn
(thank you button, for example, .btn-thank-you
) and then use $('.cd-btn:not(.btn-thank-you)')
to exclude it from click event.
Upvotes: 0
Reputation: 4208
Unbind the click event from the button.
$('.cd-btn').on('click', function(event) {
event.preventDefault();
if($(this).hasClass('.thanks')){
$('body').unbind('click','.cd-btn'); // Unbind the click event
} else {
var activePosition = $('.cd-hero-slider .selected').index();
var selectedPosition = activePosition + 1;
nextSlide($('.cd-hero-slider'), $(''), selectedPosition);
updateNavigationMarker(selectedPosition + 1);
}
});
Upvotes: 0
Reputation: 131
Add a button that removes the other buttons by setting their "display" property to "none" or use $(this).hide()
Upvotes: 0