I'm trying to understand indexOf

So I found this code on w3 schools when I was looking to figure out how to put a search bar in my dropdown box in my website. I cannot however understand how the code works at all. Everytime I think I get close to understanding it I get confused

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

So I understand that "a" is a list of all my elements and that it the first one has a value of 0 then 1, 2, 3, etc. I don't understand how I retrieve the text that I'm putting in the box and how it gets filtered. So in the txtValue string its changing the text to uppercase but how is it getting the text, and then how does it know what to filter. Also I'm very confused about the -1. If anyone could help me understand I'd greatly appreciate it.


Functionality of your code

The code you've posted loops over the elements in a and checks whether each element's text content (either textContent or innerText) contains the given filter value as a substring. The value of filter comes from the input element filter. If the element's text does contain filter as a substring, the CSS display value of the element is set to "" (default), otherwise the element is hidden by setting its CSS display value to "none".

For example if you have the a elements:

<a href="http://a.com">Great Site</a>
<a href="http://b.com">Okay Site</a>
<a href="http://c.com">Great Website</a>

And entered "great" in to the filter input. The first and last elements would have their CSS display properties set to "" and would therefore remain visible. While the middle element would have its display set to "none" and would become hidden.

indexOf

The indexOf method in JavaScript returns the index of the first occurrence of the given element in an array or string. For example 'abcb'.indexOf('b') returns 1. If the element is not found in the array, indexOf returns -1.

So the line:

if (txtValue.toUpperCase().indexOf(filter) > -1) {

really just checks if filter is a substring of txtValue.

A more straightforward way of getting the same functionality would be to use the includes method. For example:

if (txtValue.toUpperCase().includes(filter)) {

Note also that the call of toUpperCase is only done to perform the substring search in a case-insensitive way, so that "Hello" would match "hEllo".

Kendrick Lamar - i (Audio), I definition, the ninth letter of the English alphabet, a vowel. See more. Create an account or log in to Instagram - A simple, fun & creative way to capture, edit & share photos, videos & messages with friends & family.


but how is it getting the text

txtValue = a[i].textContent || a[i].innerText

This will get the text from the current item in the outer loop. The loop essentially goes through all elements in your dropdown list. Note that if textContent is empty, it will use the innerText attribute.

and then how does it know what to filter

if (txtValue.toUpperCase().indexOf(filter) > -1)

In this line, the value of filter is the search string (capitalized). The value of txtValue.toUpperCase() is the text value from above (capitalized).

Here is what the function indexOf() does taken from w3schools:

The indexOf() method returns the position of the first occurrence of a specified value in a string.

This method returns -1 if the value to search for never occurs.

Note: The indexOf() method is case sensitive.

Tip: Also look at the lastIndexOf() method.

So essentially, you are searching for the value of filter inside txtValue, and if not found, it will hide the item from the list.

HAIM - I Know Alone (Official Video), The word I is always capitalised in written English. Other forms of the pronoun, such as me and my, follow regular English capitalisation rules. I is the subject  I 1 (ī) pron. Used to refer to oneself as speaker or writer. n. pl. I's The self; the ego. [Middle English, from Old English ic; see eg in Indo-European roots.] Usage Note


In the above code variable a holds all anchor tag elements in the document (<a>...</a>). As variable a is an array and in javascript, values from an array can be accessed using indices starting from 0.

We are iterating the array using for loop and in each iteration we access

txtValue = a[i].textContent || a[i].innerText;

txtValue by accessing the array a and check for a match using string function indexOf. If a match is found it gives the position of the index of the match ( >= 0) in the string, -1 otherwise

I (Kendrick Lamar song), I definition is - the 9th letter of the English alphabet. How to use i in a sentence. me or I? Directed by S. Shankar. With Vikram, Amy Jackson, Suresh Gopi, Upen Patel. A mysterious hunchback abducts a bride on the day of her wedding, holding her hostage as he goes on a revenge spree.


I, All F and M students that study in the United States need a Form I-20, “Certificate of Eligibility for Nonimmigrant Student Status.” Once accepted into a Student  The sixteenth letter of the Hungarian alphabet, called í and written in the Latin script.


I, I-94 is a place for U.S. visitors to find travel records. î (lower case, upper case Î) The twelfth letter of the Romanian alphabet, called î or î din i and written in the Latin script.


I, Use Form I-9 to verify the identity and employment authorization of individuals hired for employment in the United States. All U.S. employers  The <i> tag defines a part of text in an alternate voice or mood. The content inside is typically displayed in italic. The <i> tag is often used to indicate a technical term, a phrase from another language, a thought, a ship name, etc. Use the <i> element only when there is not a more appropriate semantic element, such as: