I have got this error while running. I don't know why is it showing that the document.getElementById(...) is null. Is there any problem with the way it is referenced. I have read a lot and still can not look for a solution. Any small help would also be acknowledged. Thanks.
This is my code here so far.
indentation.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml"
xmlns:h = "http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<style type="text/css">
.code-str { color: #080;}
.code-elem { color: #f00;}
.code-comment { color: #00f;}
</style>
</h:head>
<h:body>
<h:form id="formId">
<p:inputTextarea rows="15" cols="80" id="text1"></p:inputTextarea>
<br/>
<h:commandButton type="button" value = "submit" action="indentation" onclick="myFunction()"></h:commandButton>
<div id="demo"></div>
</h:form>
</h:body>
<script type="text/javascript">
const keywords = {
IF: {style: "code-elem", indent: 4},
ENDIF: {style: "code-elem", indent: -4},
IFLISTING: {style: "code-str", indent: 4},
ENDIFLISTING: {style: "code-str", indent: -4},
VAR: {style: "code-comment", indent: 0},
LISTING: {style: "code-comment", indent: 0}
};
function myFunction() {
let indent = 0;
document.getElementById('formId:demo').innerHTML = document.getElementById('formId:text1').value.split(/[\r\n]+/).map(line => {
const oldIndent = indent;
line = line.trim().replace(/###([A-Z]+)(.*?)###/g, (m, keyword, arg) => {
const param = keywords[keyword];
if (!param)
return m;
indent += param.indent;
return `<span class="${param.style}">${m}</span>`;
});
return " ".repeat(Math.min(indent, oldIndent)) + line;
}).join("<br/>");
}
window.onload = myFunction;
</script>
</html>
Please, can anyone help me find the problem here? After pressing the button, it should provide some color to the code.