You should not use inline JavaScript or inline event handlers as they create spaghetti code that is hard to debug (as you have found) and (event handlers) create global function wrappers that bind this
in event handling functions to window
instead of the DOM element.
The href
attribute is really not the place to trigger JavaScript, the link's click event is the correct place in most cases.
This code works, however a DIV is already a block level element, so clicking your link won't actually do anything so I added an extra style in this example.
window.addEventListener("DOMContentLoaded", function(){
var theDiv = document.getElementById("morecontent1");
var theLink = document.getElementById("link");
theLink.addEventListener("click", function(){
theDiv.style.display = "block";
theDiv.style.border = "1px solid red"; // Just for fun!
});
});
<a href="#" id="link">Read more</a>
<div class="morecontent" id="morecontent1">Additional content 1</div>