Adding ID in list of Class

Here’s how to add ID in your list of class using jQuery

HTML

<div class="slide">hello</div>
<div class="slide">hello</div>
<div class="slide">hello</div>

JS

$(document).ready(function() {
  $(".slide").each(function(i) {
    $(this).attr('id', "slide" + (i + 1));
  });
});

CSS

#slide1,
#slide2,
#slide3 {
  height: 200px;
  width: 200px;
  border: solid red solid; 
}

Leave a Reply

Your email address will not be published. Required fields are marked *