<html>
<head>
<script>
function html(el) {
var html = el.value || '',
wysiwyg = document.getElementById('output_html');
if (html == '') {
document.getElementById('clear').disabled = true;
html = 'HTML editor!';
} else {
document.getElementById('clear').disabled = false;
}
wysiwyg.innerHTML = html;
}
function clear() {
var html = document.getElementById('input_html'),
wysiwyg = document.getElementById('output_html');
wysiwyg.innerHTML = 'HTML editor!';
html.innerHTML = '';
}
</script>
</head>
<body onload="javascript:document.getElementById('clear').disabled = false;">
<div id="output_html">HTML editor!</div>
<input id="clear" type="submit" onclick="clear()" value="Clear"/>
<textarea id="input_html" onKeyUp="html(this)" placeholder="Type HTML!"></textarea>
</body>
</html>
I wanted to learn JavaScript and fool around with it, but I came across a problem.
When I type things on the textarea, it auto updates as i want it to.
When I press on “Clear” it should clear, but it wont. Why is it doing this?
The shortest answer is don’t use inline handlers. These run with document scope, so it will try to run
document.clear()before going up one more level and finding the globalwindow.clear(), which is your function.HTML: