A little javascript help please

Hi, i have this javascript search code here:

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">
* {
  box-sizing: border-box;

#myInput {
  background-position: 10px 12px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;

#myUL {
  list-style-type: none;
  padding: 0;
  margin: 0;
  height: 168px;

#myUL li a {
  border: 1px solid #eeeeee;
  margin-top: -1px; /* Prevent double borders */
  background-color: #eeeeee;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  color: black;
  display: block

#myUL li a:hover:not(.header) {
  background-color: #42ff33;
<input type="text" id="myInput" onkeyup="myFunction()"  title="Title" placeholder="Type">
<ul id="myUL">
`<li><a href="#" target="_self" rel="noopener noreferrer">example1</a></li>`

function myFunction() {
    var input, filter, ul, li, a, i, txtValue;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    ul = document.getElementById("myUL");
    li = ul.getElementsByTagName("li");
    for (i = 0; i < li.length; i++) {
        a = li[i].getElementsByTagName("a")[0];
        txtValue = a.textContent || a.innerText;
        if (txtValue.toUpperCase().indexOf(filter) > -1) {
            li[i].style.display = "";
        } else {
            li[i].style.display = "none";


The code is working fine but i want to change it a little bit. This code shows all elements (results) (<li><a href="#" target="_self" rel="noopener noreferrer">example1</a></li>) before a type a search query. What i want to do is hiding results if search box is blank. I want to list them only with search query. Could somebody help me please? Thank you.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Hey there,

nice to meet you! :wave:

It would be awesome to see a working project on codepen.

Looking forward to helping you! :slightly_smiling_face:

1 Like

Hi, thank you, nice to meet you, too. How can you help me there?

Hey there,

we can help you, when you provide us a working project on codepen , so that you and us see the same code and can test some stuff out.

The less friction we have, e.g. caused by setting up a working environment, copying code around etc., the more help you get.