Insert a number before each div with a specific class

Viewed 66

The problem:
I've got a few divs with the same class. I want to place a number (start from 2 to --) before each div.

This is what I tried:

let optionNumber = $(".product-configure-custom-option")
              $('.product-configure-custom').each(function () {
              if ( $(this).find("div.product-configure-custom-option").length ) {
                $('<div class="title-number float-left text-center mr-2">' + $(optionNumber).length + '</div>').insertBefore(optionNumber)
              } 
            });
.product-configure-custom-option{
float:right;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
        <div class="product-configure-custom">
            <div class="product-configure-custom-option">Option 2</div>
            <div class="product-configure-custom-option">Option 3</div>
            <div class="product-configure-custom-option">Option 4</div>
            <div class="product-configure-custom-option">Option 5</div>
            <div class="product-configure-custom-option">Option 6</div>
            <div class="product-configure-custom-option">Option 7</div>
            </div>
</div>

What I want the result to be:

  1. Option Two
  2. Option Three
  3. Option Four
4 Answers

I think you want something like this:

var start = 2;

$(document).ready(function() {
  $('.product-configure-custom-option').each(function() {
    console.log(this.innerHTML);
    this.innerHTML = start + '. ' + this.innerHTML;
    start++;
    });
});

Although the CSS needs looking at after that.

Looks something like this: https://jsfiddle.net/qzhtr80d/2/

At the moment you're generating a single extra <div> for every <div> found. A more elegant way would be getting the text content of every <div> directly and prepend the counter there.

$(this).find("div.product-configure-custom-option")

returns an object we can use to iterate over the individual elements using a for-loop and do the changes via plain JavaScript. The child element can be queried by the .firstChild property and the actual text is accessible using .data.

Here's an example:

$('.product-configure-custom').each(function() {
  let elements = $(this).find("div.product-configure-custom-option");
  for (var a = 0; a < elements.length; a++) {
    elements[a].firstChild.data = (a + 2) + ". " + elements[a].firstChild.data;
  }
});
.product-configure-custom-option {
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="product-configure-custom">
    <div class="product-configure-custom-option">Option 2</div>
    <div class="product-configure-custom-option">Option 3</div>
    <div class="product-configure-custom-option">Option 4</div>
    <div class="product-configure-custom-option">Option 5</div>
    <div class="product-configure-custom-option">Option 6</div>
    <div class="product-configure-custom-option">Option 7</div>
  </div>
</div>

let optionsContainer = $(".product-configure-custom");
optionsContainer.each(function() {
    let options = $(this).find("div.product-configure-custom-option");
    if (options.length) {
        options.each(function(index, option) {
            let startValue = 2;
            let incrementedValue = startValue + index;
            $('<div class="title-number float-left text-center mr-2">' + incrementedValue + '</div>').insertBefore(option);
        });
    }
});
        
.product-configure-custom-option{
    float:right;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
        <div class="product-configure-custom">
            <div class="product-configure-custom-option">Option 2</div>
            <div class="product-configure-custom-option">Option 3</div>
            <div class="product-configure-custom-option">Option 4</div>
            <div class="product-configure-custom-option">Option 5</div>
            <div class="product-configure-custom-option">Option 6</div>
            <div class="product-configure-custom-option">Option 7</div>
            </div>
</div>

Not the answer you're asking for, but you can do this in plain CSS: MDN Using CSS counters

.product-configure-custom {
  counter-set: foo 1;
}

.product-configure-custom-option::before {
  counter-increment: foo;
  content: counter(foo) ". ";
}
<div class="wrapper">
  <div class="product-configure-custom">
    <div class="product-configure-custom-option">Option 2</div>
    <div class="product-configure-custom-option">Option 3</div>
    <div class="product-configure-custom-option">Option 4</div>
    <div class="product-configure-custom-option">Option 5</div>
    <div class="product-configure-custom-option">Option 6</div>
    <div class="product-configure-custom-option">Option 7</div>
  </div>
</div>

Related