JavaScript Looping through html elements with the same class and finding the order of elements looping through htmlcollection,looping through html elements javascript,looping through

I have created an image slider that has 3 images and 3 dots, each dot runs a function that loads an image, the first dot the first image etc.

Now what I'm asking is how can I create a loop for the javascript to do the same result instead of repeating myself for each button?

<span class="kcslider-dot" id="dot-1"></span> 
<span class="kcslider-dot" id="dot-2"></span> 
<span class="kcslider-dot" id="dot-3"></span> 

$("#dot-1").click(function(){
  currentSlide(1);
})

$("#dot-2").click(function(){
  currentSlide(2);
})

$("#dot-3").click(function(){
  currentSlide(3);
})
Answer:1

Another option is to use jQuery Data

<span class="kcslider-dot" data-slide-id="1" id="dot-1"></span>
<span class="kcslider-dot" data-slide-id="2" id="dot-2"></span>
<span class="kcslider-dot" data-slide-id="3" id="dot-3"></span>

$(".kcslider-dot").click(function(){
  var slideId = $(this).data('slide-id');

  currentSlide(slideId);
});
Answer:2

You can use [id^='dot-'] selector to select all the elements with id value starting with dot- then inside the click function get the numeric part after hyphen by doing split() on the idvalue:

USING jQuery

$("[id^='dot-']").click(function(){
  var idNumber = this.id.split('-')[1];
  currentSlide(idNumber);
});
function currentSlide(num){
   console.log(num);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="kcslider-dot" id="dot-1">1</span> 
<span class="kcslider-dot" id="dot-2">2</span> 
<span class="kcslider-dot" id="dot-3">3</span>
Answer:3

You should use your class in the selector of jquery and then loop with the function "each". You can use a var that acts as counter o use an attribute in the html for get the parameter of the function "currentSlide"

var i = 1
$('span.kcslider-dot').each(function(){
     currentSlide(i);
     i++;
})
Answer:4

As long as all of your slides belong to the same class you can use class-selector instead of id. When you select all slides the output of the selection will be an array and the order will be the same as in the DOM. So after the selection you just may loop through that array and add listener to each element separately specifying certain slide using index:

let sliders = $(".kcslider-dot");
for(let i = 0; i < sliders.length; i++) {
    sliders[i].click( () => {
        currentSlide(i+1);
    })
}

Cheers.

Answer:5

If the span elements are the only children of their parent you could use the jQuery .index() function to load the corresponding image.

Also you can attach the click event to the class instead of the id. That way you only need a single event handler.

e.g.

function currentSlide(index) {
  console.log(index);
}

$(".kcslider-dot").click(function() {
  currentSlide($(this).index() + 1);
});
.kcslider-dot {
  display: inline-block;
  width: 15px;
  height: 15px;
  background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <span class="kcslider-dot" id="dot-1"></span>
  <span class="kcslider-dot" id="dot-2"></span>
  <span class="kcslider-dot" id="dot-3"></span>
</div>
Answer:6

I'm using following JS code to add extra information[LINK] to clipboard while copying the text from website: $(document).on('copy', function(e) { var sel = window....

I'm using following JS code to add extra information[LINK] to clipboard while copying the text from website: $(document).on('copy', function(e) { var sel = window....

  1. text formatting include

I have found a few examples out there but having a hard time getting any to work, if anyone had a clearer or some insight would be much appreciated. var wrap = function() { var self = d3.select(this),...

I have found a few examples out there but having a hard time getting any to work, if anyone had a clearer or some insight would be much appreciated. var wrap = function() { var self = d3.select(this),...

  1. ellipsis long text css
  2. long text ellipsis
  3. javascript ellipsis for long text
  4. html ellipsis for long text
  5. ellipsis for long text css

How do I keep only one definition visible at the same time in this example: In other words: Clicking on i button should toggle class on itself and the definition below the term, and remove ...

How do I keep only one definition visible at the same time in this example: In other words: Clicking on i button should toggle class on itself and the definition below the term, and remove ...

  1. remove class click anywhere
  2. remove class click jquery
  3. remove class click outside
  4. remove class click on body
  5. remove class click
  6. remove class click event
  7. remove class on click javascript
  8. remove class after click outside the div
  9. remove class when click outside div
  10. remove class on click angular 6
  11. remove class when click outside jquery
  12. remove class on click js
  13. remove class on click react
  14. remove class on click angular
  15. remove class after click jquery
  16. jquery remove class click event
  17. add remove class click jquery
  18. remove class after click javascript
  19. remove class on click angular 2
  20. remove class on click angular 4

Anchor tag not downloading file in IE, instead it gives the option to search for app in app store to open file. For chrome and FF this code is working fine. I don't know this is happening in windows 7 ...

Anchor tag not downloading file in IE, instead it gives the option to search for app in app store to open file. For chrome and FF this code is working fine. I don't know this is happening in windows 7 ...

  1. dynamically create anchor tag in javascript
  2. dynamically create anchor tag in c#
  3. dynamically create anchor tag jquery