When i was on PHP interview they gave me test with PHP,JavaScript. One of the questions from JS was to get some random div ( and to do something with it, i cant remember what). I didn't get any html code and i needed to write just JS. Now i'm learning JS and i try to find solution for the question.
I try this
var node = document.getElementsByTagName('div');
var divLength=node.length;
var randomDiv=Math.random()*divLength;
and now i'm testing with some code
<html>
<head>
<script>
var node = document.getElementsByTagName('div');
var divLength=node.length;
alert("There are "+divLength+" div tags in the html code");
var randomDiv=Math.random()*divLength;
</script>
</head>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</body>
</html>
but when i run the result is: "There are 0 div tags in the html code"
also i tried
var node=document.querySelectorAll("div");
but the result is the same.
<script>after the<div>elements.window.onload = function(){/*your code here*/};var all = document.querySelectorAll("*");console.log(all[parseInt(Math.random()*all.length,10)]);