JavaScript Display randomly selected items

I have 5 boxes and an array of items. How do I display 5 items in 5 boxes, randomly selected without repetition?

Is it a problem with:

ptags[index].textContent = item[0].label;

that it is not displaying in the box?

I tried many ways without success. I use DreamWeaver as IDE, might there be a problem with that?

var array2 = [];
var items = [{
    label: '1',
    url: '1.png'
  },
  {
    label: '2',
    url: '2.png'
  },
  {
    label: '3',
    url: '3.png'
  },
  {
    label: '4',
    url: '4.png'
  },
  {
    label: '5',
    url: '5.png'
  },
  {
    label: '6',
    url: '6.png'
  },
  {
    label: '7',
    url: '7.png'
  },
  {
    label: '8',
    url: '8.png'
  },
  {
    label: '9',
    url: '9.png'
  },
  {
    label: '10',
    url: '10.png'
  },
  {
    label: '11',
    url: '11.png'
  },
  {
    label: '12',
    url: '12.png'
  }
];

array2 = items.slice();
console.log(array2);
rvalue();

var item;

function rvalue() {
  ptags = document.querySelectorAll('[name="values"]');
  console.log(ptags);
  for (var index = 0; index < 5; index++) {
    var randomIndex = Math.floor(Math.random() * array2.length);
    item = array2.splice(randomIndex, 1);
    console.log(item);

    ptags[index].textContent = item[0].label;

    ptags[index].dataset.itemIndex = randomIndex;
  }

}

console.log(array2);
body {
  overflow: hidden;
  background-size: 100vw 100vh;
}

#container {
  white-space: nowrap;
  text-align: center;
  border: px solid #CC0000;
  margin: 2px;
  margin-right: 2px;
}

.box {
  width: calc(15.4% - 4px);
  height: 15vh;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 5px;
  border: 2px solid #333;
  border: #000 border-color: #e6e600;
  margin: -2px;
  background-color: #0F6;
}

.box p {
  font-size: calc(2vw + 10px);
}

p {
  font: "Courier New", Courier, monospace;
  font-size: 30px;
  color: #005ce6;
  text-align: center;
}

.text {
  padding: 20px;
  margin: 7 px;
  margin-top: 10px;
  color: white;
  font-weight: bold;
  text-align: center;
}
<div id="container">
  <div class="box" id="10">
    <p name="values"></p>
  </div>
  <div class="box" id="11">
    <p name="values"></p>
  </div>
  <div class="box" id="12">
    <p name="values"></p>
  </div>
  <div class="box" id="13">
    <p name="values"></p>
  </div>
  <div class="box" id="14">
    <p name="values"></p>
  </div>
</div>
Answer:1

You've a correct code you need just to put the code inside a ready function or just move it to the end of the body after the HTML tags to make sure the DOM was loaded before the script run.

For that there's severals solution I suggest the use of the DOMContentLoaded event :

document.addEventListener("DOMContentLoaded", function() {
    //Your code here 
});

NOTE: To detect such bugs the next time by your self you could check the length of the result first like :

console.log( document.querySelectorAll('[name="values"]').length );

If the length is zero and you have a correct selector then you need to check the DOM availability.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Untitled Document</title>

  <style>
    body {
      overflow: hidden;
    }
    
    .heading {
      margin-left: 20%;
      margin-right: 20%;
      margin-top: -2%;
    }
    
    .box {
      width: calc(15.4% - 4px);
      display: inline-block;
      border-radius: 5px;
      border: 2px solid #333;
      border: #000 border-color: #e6e600;
      margin: -2px;
      background-color: #0F6;
    }
    
    .box {
      height: 15vh;
      display: inline-flex;
      align-items: center;
      justify-content: center
    }
    
    #container {
      white-space: nowrap;
      text-align: center;
      border: px solid #CC0000;
      margin: 2px;
      margin-right: 2px;
    }
    
    .box p {
      font-size: calc(2vw + 10px);
    }
    
    p {
      font: "Courier New", Courier, monospace;
      font-size: 30px;
      color: #005ce6;
      text-align: center;
    }
    
    .text {
      padding: 20px;
      margin: 7 px;
      margin-top: 10px;
      color: white;
      font-weight: bold;
      text-align: center;
    }
    
    body {
      background-size: 100vw 100vh;
    }
  </style>
</head>

<body>
  <script>
    document.addEventListener("DOMContentLoaded", function() {
      var array2 = [];
      var items = [{
          label: '1',
          url: '1.png'
        },
        {
          label: '2',
          url: '2.png'
        },
        {
          label: '3',
          url: '3.png'
        },
        {
          label: '4',
          url: '4.png'
        },
        {
          label: '5',
          url: '5.png'
        },
        {
          label: '6',
          url: '6.png'
        },
        {
          label: '7',
          url: '7.png'
        },
        {
          label: '8',
          url: '8.png'
        },
        {
          label: '9',
          url: '9.png'
        },
        {
          label: '10',
          url: '10.png'
        },
        {
          label: '11',
          url: '11.png'
        },
        {
          label: '12',
          url: '12.png'
        }
      ];

      array2 = items.slice();
      rvalue();

      var item;

      function rvalue() {
        ptags = document.querySelectorAll('[name="values"]');

        for (var index = 0; index < 5; index++) {
          var randomIndex = Math.floor(Math.random() * array2.length);
          item = array2.splice(randomIndex, 1);

          ptags[index].textContent = item[0].label;

          ptags[index].dataset.itemIndex = randomIndex;
        }

      }
    });
  </script>


  <div id="container">

    <div class="box" id="10">
      <p name="values"></p>
    </div>
    <div class="box" id="11">
      <p name="values"></p>
    </div>
    <div class="box" id="12">
      <p name="values"></p>
    </div>
    <div class="box" id="13">
      <p name="values"></p>
    </div>
    <div class="box" id="14">
      <p name="values"></p>
    </div>

  </div>
</body>

</html>
Answer:2

Often in the interview questions it is heard that Why one should use redux form in the ReactJs instead of simple form. which extra ordinary facilities it provides to the developer ? Then what should ...

Often in the interview questions it is heard that Why one should use redux form in the ReactJs instead of simple form. which extra ordinary facilities it provides to the developer ? Then what should ...

  1. redux form simple example
  2. redux form simple
  3. react redux simple form example
  4. react redux simple form

I am trying to use phantomjs to automate the process of login into facebook. The issue is that facebook uses js codes to set cookies before the login form is submitted. Clicking on the email input box ...

I am trying to use phantomjs to automate the process of login into facebook. The issue is that facebook uses js codes to set cookies before the login form is submitted. Clicking on the email input box ...

  1. phantomjs read cookies

var j = jQuery.noConflict(); j.fn.portionHeight = function(ratio){ var width = j(this).width(); var height = Math.floor(width*ratio); j(this).height(height); } function initAndResize(){ ...

var j = jQuery.noConflict(); j.fn.portionHeight = function(ratio){ var width = j(this).width(); var height = Math.floor(width*ratio); j(this).height(height); } function initAndResize(){ ...

I was looking at this code example and stumbled upon this syntax: import React, { Component } from 'react'; const PrivateRoute = ({component:Component, ...rest}) => ( //(...) <Component {......

I was looking at this code example and stumbled upon this syntax: import React, { Component } from 'react'; const PrivateRoute = ({component:Component, ...rest}) => ( //(...) <Component {......

  1. this type annotation typescript
  2. this type annotation