locq
locq

Reputation: 301

Adding styling to randomly generated Javascript numbers

I'm trying to add styling (css) and have the ability to place some randomly generated numbers(%) anywhere I want (in a <div> to be more specific), but I don't know to achieve this.

These numbers are currently being placed at the complete end of each list items ( outside of main <div> but inside of the <li> ).

For clarity, here's a screenshot of 2 of the list items and how the number are being displayed: https://imgur.com/gsGRlaT

The html where the numbers are currently being displayed:

<div>
   {% if  page_obj.object_list %}
       <ol class="row top20" id="my_list">

          {% for result in page_obj.object_list %}

          <li id="list_item">
              <div class="showcase col-sm-6 col-md-4">
               <a href="{{ result.object.get_absolute_url }}">
                      <h3>{{result.object.title}}</h3>
                      <h5>{{ result.object.destination }}</h5>
                      <img src="{{ result.object.image }}" class="img-responsive">
               </a>
               </div>

          <!--  Numbers are displayed randomly here --> 90%

          <!--  But I'm trying to display them in the div below -->

            <div class="numbers"> 90% </div>

          </li>

           {% endfor %}

       </ol>
</div>
    {% endif %}

And the Javascript code that generate the numbers:

<script type="text/javascript">

  var orderedList = document.getElementById("my_list");
  var itemLength = 8; //REPLACE THIS WITH THE LENGTH OF THE ITEM LIST

  function getRandomInt(min, max) {
      min = Math.ceil(min);
      max = Math.floor(max);
      return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  // get a default 100 into the array
  var arrayOfNumbers = [100],
      listItem = document.getElementById("list_item");

  // get itemLength random numbers
  for (let i = 0; i < itemLength; i++) {
    arrayOfNumbers.push(getRandomInt(30, 100))
  }

  // sort the array of random numbers
  arrayOfNumbers.sort(function(a, b) {
    return b - a
  })

  // now do the lopping and creating elements
  for (let i = 0; i < arrayOfNumbers.length; i++) {
    let randomInt = document.createTextNode(arrayOfNumbers[i] + "%");
    listItem = document.getElementById("list_item");
    listItem.appendChild(randomInt);
    orderedList.appendChild(listItem);
  }

</script>

So how can I take control of how and where these numbers are placed?

Upvotes: 0

Views: 52

Answers (1)

Sudipto Roy
Sudipto Roy

Reputation: 1046

You are almost to the solution. Just a few minor modifications.

Create a div in place of where you create text node and add the class numbers to the created div. And you are done.

for (let i = 0; i < arrayOfNumbers.length; i++) {
    let randomIntContainer = document.createElement('div');
    randomIntContainer.textContent = arrayOfNumbers[i] + "%";
    randomIntContainer.setAttribute('class', 'number');
    listItem = document.getElementById("list_item");
    listItem.appendChild(randomIntContainer);
    orderedList.appendChild(listItem);
  }

Upvotes: 1

Related Questions