Started learning web dev and stumbled onto something simple I've been breaking my head over the past hour. It's simple code to show the border color of a div
element with an alert
. It works. But when I move the style from inline to a style
element, it returns nothing.
Working code:
function myFunction() {
alert(document.getElementById("myDiv").style.borderColor);
}
<div id="myDiv" style="border:thick solid green">This is a div.</div>
<br>
<button type="button" onclick="myFunction()">Return border color</button>
non-working code:
function myFunction() {
alert(document.getElementById("myDiv").style.borderColor);
}
<style>
#myDiv {
border: thick solid green;
}
</style>
<div id="myDiv">This is a div.</div>
<br>
<button type="button" onclick="myFunction()">Return border color</button>
Only thing I changed is move the style attribute from the div tag to the head. Why did it stop working and how do I fix it?