I would like to replace for loop with a forEach
function as the loop is not working in Splunk JavaScript.
The reason the for loop is not working is that my JavaScript code is embedded in XML, and when I use the <
or >
characters in my JavaScript code, I get an error due to them.
function myFunction() {
// Declare variables
var input, filter, ul, li, a, i;
input = document.getElementById("mySearch");
filter = input.value.toUpperCase();
ul = document.getElementById("myMenu");
li = ul.getElementsByTagName("li");
// Loop through all list items, and hide those who don't match the search query
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
<input type="text" id="mySearch" onkeyup="myFunction()" placeholder="Search.." title="Type in a category">
<ul id="myMenu">
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
<li><a href="#">JavaScript</a></li>
<li><a href="#">PHP</a></li>
<li><a href="#">Python</a></li>
<li><a href="#">jQuery</a></li>
<li><a href="#">SQL</a></li>
<li><a href="#">Bootstrap</a></li>
<li><a href="#">Node.js</a></li>
</ul>
It seems like you have an XML escaping issue, since your JavaScript is embedded in an XML document in your Splunk setup. Instead of rewriting your JavaScript to not use the
<
and>
characters, it would make more sense to escape these characters properly in the XML so you can use them normally.You can use a
CDATA
section to let the XML parser know that you are writing plain text and not XML so that it doesn't choke on the<
and>
characters. To make aCDATA
section, just wrap your code like this in the XML document:This tells the XML reader that everything in between these tags is to be taken literally and not to try to be parsed as XML. It should keep the XML parser from complaining that you have erroneous
<
and>
characters.For more information about escaping values in XML, see this Stack Overflow answer:
What characters do I need to escape in XML documents?