JavaScript Generate random number of divs generate random number python,generate random number java,generate random

I can't make the script to create random amount of divs. In this specific example between 5 and 20. The problem is in the for loop maybe? The function that generates random numbers is working correctly below in the random color function, I guess it is not recognized for some reasons. Also I am not getting any errors in firebug.

Example:

function generateDiv(){
    var dfrag = document.createDocumentFragment();
    var count = generateRandom(5, 20);
    var i=0;
    for (var i = 0; i < count; i++){
        var div = document.createElement("div");
        dfrag.appendChild(div);
    }
}
var divs = document.getElementsByTagName("div");
for (i = 0; i < divs.length; i++) {
    div = divs[i];
    alterDivStyle(div);
}
function rndColor() {
    var r = ('0' + generateRandom(0,255).toString(16)).substr(-2), // red
          g = ('0' + generateRandom(0,255).toString(16)).substr(-2), // green
          b = ('0' + generateRandom(0,255).toString(16)).substr(-2); // blue
    return '#' + r + g + b;
}

function generateRandom(min, max) {
    var number = Math.floor(Math.random() * (max - min )) + min;
    return number;
}
function alterDivStyle(div){
    div.style.width = generateRandom(20, 100) +"px";
    div.style.height = generateRandom(20, 100) +"px";
        div.style.backgroundColor = rndColor();
    div.style.color = rndColor();
    div.style.position = "absolute";
    div.style.border = "solid";
    div.style.borderColor = rndColor();
    div.style.borderWidth = rndColor();
    div.style.borderRadius = generateRandom(0, 10)+"px";
    div.innerHTML = "<strong>div</strong>";
};
Answer:1

You never added the document fragment to the DOM

"DocumentFragments are DOM Nodes. They are never part of the main DOM tree. The usual use case is to create the document fragment, append elements to the document fragment and then append the document fragment to the DOM tree. In the DOM tree, the document fragment is replaced by all its children."

https://developer.mozilla.org/en-US/docs/Web/API/document.createDocumentFragment

Answer:2

1) You closed the "generateDiv function" in the wrong place, move the closing bracket from before the "var divs" to after the "var divs" loop.

2) You are altering all the existing divs in the DOM: var divs = document.getElementsByTagName("div"); Don't do that, use dfrag.

3) Like Matthew said, you have to add dfrag to the DOM.

Answer:3

I am trying to implement drag and drop rows with in same table using html5, but I would like to restrict the drop area to few rows instead of whole table. Code : <table id="visibletable" class="...

I am trying to implement drag and drop rows with in same table using html5, but I would like to restrict the drop area to few rows instead of whole table. Code : <table id="visibletable" class="...

due to CRM 2011 online problems with IE10, we have decided to convert all front-end JavaScript from Soap to oData. Everything was going fine, I installed the oData designer, centralized the queries in ...

due to CRM 2011 online problems with IE10, we have decided to convert all front-end JavaScript from Soap to oData. Everything was going fine, I installed the oData designer, centralized the queries in ...

I have created a Button in Bootstrap and assigning an attribute to it. Upon clicking the button, I wish to get the attribute value in the function. However, when I pass $element.currentTarget to ...

I have created a Button in Bootstrap and assigning an attribute to it. Upon clicking the button, I wish to get the attribute value in the function. However, when I pass $element.currentTarget to ...

  1. button attributes bootstrap

I have this code and I cannot get the second time to format properly: setInterval(function() { var local = new Date(); var localdatetime = local.getHours() + ":" + local.getMinutes() + ":" +...

I have this code and I cannot get the second time to format properly: setInterval(function() { var local = new Date(); var localdatetime = local.getHours() + ":" + local.getMinutes() + ":" +...

  1. format javascript date object
  2. format javascript date string
  3. format javascript date time
  4. format javascript date mm/dd/yyyy
  5. format javascript date output
  6. format javascript date yyyy-mm-dd
  7. format javascript date iso 8601
  8. format javascript date dd/mm/yyyy
  9. format javascript date with moment
  10. format javascript date.now
  11. javascript format date with timezone
  12. javascript format date locale
  13. javascript format date from timestamp
  14. javascript format date time string
  15. javascript format date iso
  16. javascript format date function
  17. javascript format date month name
  18. javascript format date utc
  19. javascript format date dd mmm yyyy
  20. javascript format date without time