0

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>
Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
  • What do you mean by "is not working" here. The `for` loop is basic JS. Not that I don't try to get rid of them whenever I can, but still, how isn't it working? – Scott Sauyet Sep 25 '18 at 20:47
  • 1
    You can find [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) the doc of forEach in javascript. – AndrasCsanyi Sep 25 '18 at 20:48
  • @ScottSauyet Hello, Thanks for your comment. I'm not applying it on regular javascript environment. There is a software called Splunk that is very sensitive to include certain javascript inside the XML file. Thanks – Anmar Al Shammari Sep 25 '18 at 20:59
  • 1
    Please also show simplified HTML that this code operates on. – connexo Sep 25 '18 at 21:03
  • 1
    Please add that, including proper formatting, to your question, not in the comments. – connexo Sep 25 '18 at 21:05
  • i just did. It is basically search menu in menu content. Please refer to https://www.w3schools.com/howto/howto_js_search_menu.asp the same thing but instead of for loop i want for each @connexo – Anmar Al Shammari Sep 25 '18 at 21:07
  • What error from Splunk, specifically, are you getting when you try to use a `for` loop? If a `for` loop doesn't work, there's really no reason to believe that `forEach` would be any better. Does Splunk complain specifically with an error message about the for loop? – Maximillian Laumeister Sep 25 '18 at 21:10
  • Yes, it complains about the > operator. it doesn't take it shows it as validation for some reason. @MaximillianLaumeister – Anmar Al Shammari Sep 25 '18 at 21:11
  • thanks @connexo for editing! – Anmar Al Shammari Sep 25 '18 at 21:13
  • @AnmarAlShammari Without knowing a whole lot about Splunk, is there a way you can escape the character or even the whole code block, basically telling Splunk that it's a literal string and not to try to complain about special characters? – Maximillian Laumeister Sep 25 '18 at 21:13
  • 1
    I'm only asking because this seems like it might be an [X Y problem](https://meta.stackexchange.com/a/66378/299116). Instead of developing your entire app without using angle bracket characters, it might make more sense to figure out why Splunk is choking on the angle bracket characters in the first place. – Maximillian Laumeister Sep 25 '18 at 21:17
  • @MaximillianLaumeister i'm including this code in an XML file wrapped up with html tags. it doesn't accept any the "<" ">" operator. is there anyway we can make this work with out the operator? thanks! – Anmar Al Shammari Sep 25 '18 at 21:17
  • Try [escaping the ">" character](https://stackoverflow.com/a/1091953/2234742) by using ">" in the XML instead of ">" directly. Might work. – Maximillian Laumeister Sep 25 '18 at 21:20
  • I can't mix javacsript with XML operator!! can you show me? @MaximillianLaumeister – Anmar Al Shammari Sep 25 '18 at 21:22

1 Answers1

0

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 a CDATA section, just wrap your code like this in the XML document:

<![CDATA[
    ...your code here...
]]>

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?

Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
  • @AnmarAlShammari I recommend you post your XML in your question so that StackOverflow users are able to help see what's wrong with it and integrate the fix properly. I also recommend you post the specific error that you are getting (is it the exact same error as before, or a different error?) – Maximillian Laumeister Sep 25 '18 at 23:15
  • @AnmarAlShammari: Again, "it doesn't work" is not a useful description. From what you described so far, this sounds like the most likely thing to help. Have you read about CDATA sections to make sure you know how to use them properly? Now that you have the hint here, have you searched for how to escape `<` characters in XML? As Maximillian said, please post (some reasonable subset of) your XML to help others help you diagnose the issue. – Scott Sauyet Sep 25 '18 at 23:33