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; 
}

Add ascending numbers to ID

Here’s how to dynamically append a new data using input field.

HTML

<form id="create_menu_item">
 <h4>Add Menu Item</h4>
 <div>
   <input id="new_menu_name" type="text" name="new_menu_name" placeholder="Please enter your menu title" />
   <input class="add_menu_item" name="add_menu_item" value="Add" type="button" />
 </div>
 <h4>Menu Items</h4>
 <div id="created_buttons">
   <ol id="created_buttons_list"> </ol>
 </div>
</form>

JS

$(".add_menu_item").click((function() {
 var id = 0;
 return function() {
   var value = $(this).prev().val();
   if (value.length) {
     var newDiv = $('<div id="toggleshow" class="div_menu_button"></div>');
     $('#created_buttons_list').append(newDiv.val(value).text(value));
     newDiv.wrap("<li></li>");
     newDiv.after(showDiv);
     id++;
   }
 }
})());