Home > front end >  toLocaleLowerCase() is not a function
toLocaleLowerCase() is not a function

Time:03-20

when I use this function it gives me an error that it is not a function.

<div id="cards_main"></div>
let arry = new Array();
for (i = 0; i < 2; i  ) {
  if (document.getElementsByTagName("input")[i].value != "") {
    for (cards of arry) {
      if (cards.naming.toLocaleLowerCase().startsWith(document.getElementsByTagName("input")[i]).toLocaleLowerCase()) {
        document.querySelector("#cards_main").innerHTML = "";
        addCard(cards);
      }
    }
  }
}

Note that the array is not empty

CodePudding user response:

There are a few issues in your code.

The .startsWith() method returns a boolean of true or false, and expects a string. In your case you are passing a HTML element to it.

Try instead:

.startsWith(document.getElementsByTagName("input")[i].value)

.toLocaleLowerCase() should be run on a string type, but you are running it on a boolean.

if (cards
      .naming
      .toLocaleLowerCase()
      .startsWith(document.getElementsByTagName("input")[i]) // This returns a boolean
      .toLocaleLowerCase() // But this line isn't valid on a boolean
    ) {

You likely wanted to call toLocaleLowerCase on your input value.

Try this code instead:

const inputValue = document.getElementsByTagName("input")[i].value;
if (cards.naming.toLocaleLowerCase().startsWith(inputValue.toLocaleLowerCase())) {
  // ...
}
  • Related