Why does alert(p1) show null?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="js/product.js"></script>
</head>
<body>
Device:<select id="device" name="device" style="width:250px;"> </select> Line:<select id="line" name="line" style="width:250px;"> </select>
</body>
</html>
window.onload = load();
function load() {
var p1 = document.getElementById("device");
var l1 = document.getElementById("line");
alert(p1);
alert(l1);
}
You need to change this line:
To read the following:
Note the difference, the first example will invoke the
load()function, and assign its return value towindow.onload, which of course will beundefined.The second example will assign the function itself to
window.onload, which is what you want, and will alert[object HTMLSelectElement](in Firefox anyways).