This particular code can be inside the body of an HTML file, and this code is executed before certain HTML elements are created. Thus, a value is undefinedassigned to these values.
So, you can move the value assignment part inside the function itself.
var hours, mins, seconds;
function random()
{
hours = document.getElementById("hrs").value;
mins = document.getElementById("min").value;
seconds = document.getElementById("sec").value;
alert(hours);
alert(mins);
alert(seconds);
}
Note 1: Normally, if you are not using the jQuery library, code like this is put in onload. This is evidenced by MDN,when onload is triggered
. , DOM, .
, HTML, , , HTML .
2: , , id HTML hrs, min sec. name id .