Darko Darko - 14 days ago 5
jQuery Question

How to skip elements in custom vertical slider based on input value

I'm trying to skip some elements with class "skip", based on input value, but it's not working OK when input value "No" is selected.

$("input:radio[name='q1']").change(function() {
if ($(this).val() == 'Yes') {
$('.skip').removeClass('hidden');
} else {
$('.skip').addClass('hidden');
}
});


here's the complete code in fiddle

Answer

here i think this is what you were trying to do:

https://jsfiddle.net/jfojv01e/

the problems were:

  1. you were using .next() only gives the immediate sibling so use .nextAll instead

Given a jQuery object that represents a set of DOM elements, the .next() method allows us to search through the immediately following sibling of these elements in the DOM tree and construct a new jQuery object from the matching elements

  1. Your condition was backwords it should be $(this).val() == 'No'
  2. You werent skiping the hidden elements so it was still finding them... see the :not selector i added $currentItem.nextAll('.scrollStep:not(.hidden)')