Since there isn't a fully-working example, here is one where I've set the html
and body
tags to be 100%
width and height, which make it clickable anywhere on the screen.
Then, I use EventTarget.addEventListener()
instead of inline event handlers (e.g., onclick=''
), which are better to use in most cases.
And last, I create the p
element (which I assume your addP()
function is doing) using the document.createElement()
DOM method, add text to it, then append it to the document.body
element with Node.appendChild()
.
Keep in mind that, in practice, you generally do not ever want to add things like click handlers or mouse event handlers to the body
element, since it will trigger on any of those events on the page. Instead, use an element that's a parent of the element, or document.addEventListener()
.
<!doctype html>
<html>
<head>
<title></title>
<style>
html, body {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
}
</style>
<script>
window.addEventListener('load', function ol(){
document.body.addEventListener('click', function cl(){
var p = document.createElement('p');
p.textContent = 'Hello World!';
document.body.appendChild(p);
});
});
</script>
</head>
<body>
</body>
</html>
http://jsfiddle.net/m3ssssvp/2/
test
and click on the word test your function should trigger. – NewToJS May 25 '15 at 01:10