Categories
JavaScript Answers

How to add JavaScript autocomplete without an external library?

Sometimes, we want to add JavaScript autocomplete without an external library.

In this article, we’ll look at how to add JavaScript autocomplete without an external library.

How to add JavaScript autocomplete without an external library?

To add JavaScript autocomplete without an external library, we add the datalist element.

For instance, we write

<datalist id="languages">
  <option value="HTML"></option>
  <option value="CSS"></option>
  <option value="JavaScript"></option>
  <option value="Java"></option>
  <option value="Ruby"></option>
</datalist>

<input type="text" list="languages" />

to add the datalist element with id attribute value that matches the list attribute value of the input.

As a result, the datalist’s options are shown when we type in something into the input that matches the options.

Conclusion

To add JavaScript autocomplete without an external library, we add the datalist element.

Categories
JavaScript Answers

How to connect client to server using Socket.io and JavaScript?

Sometimes, we want to connect client to server using Socket.io and JavaScript.

in this article, we’ll look at how to connect client to server using Socket.io and JavaScript.

How to connect client to server using Socket.io and JavaScript?

To connect client to server using Socket.io and JavaScript, we call the connect method.

For instance, we write

<script src="/socket.io/socket.io.js"></script>

to add the socket.io script.

Then we write

const socket = io.connect();

to call io.connect to start the connection.

Conclusion

To connect client to server using Socket.io and JavaScript, we call the connect method.

Categories
JavaScript Answers

How to fix SyntaxError: missing ) after argument list with JavaScript?

Sometimes, we want to fix SyntaxError: missing ) after argument list with JavaScript.

In this article, we’ll look at how to fix SyntaxError: missing ) after argument list with JavaScript.

How to fix SyntaxError: missing ) after argument list with JavaScript?

To fix SyntaxError: missing ) after argument list with JavaScript, we should make sure the open and closing parentheses match.

For instance, we write

const navs = document.getElementsByClassName("nav-coll");

for (const nav of navs) {
  nav.addEventListener(
    "click",
    () => {
      console.log("clicked");
    },
    false
  );
}

to call getEleemntsByClassName, addEventListener and cnsole.log with matching opening and closing parentheses to clear the error.

Conclusion

To fix SyntaxError: missing ) after argument list with JavaScript, we should make sure the open and closing parentheses match.

Categories
CSS

How to allow scroll but hide scrollbar with CSS?

Sometimes, we want to allow scroll but hide scrollbar with CSS.

In this article, we’ll look at how to allow scroll but hide scrollbar with CSS.

How to allow scroll but hide scrollbar with CSS?

To allow scroll but hide scrollbar with CSS, we select the ::-webkit-scrollbar pseudo-element.

For instance, we write

::-webkit-scrollbar {
  display: none;
}

to hide the scrollbar by selecting ::-webkit-scrollbar and set the display property to none.

Conclusion

To allow scroll but hide scrollbar with CSS, we select the ::-webkit-scrollbar pseudo-element.

Categories
TypeScript Answers

How to map Typescript enums?

Sometimes, we want to map Typescript enums.

In this article, we’ll look at how to map Typescript enums.

How to map Typescript enums?

To map Typescript enums, we get the keys and map them.

For instance, we write

const mapped = (Object.keys(MyEnum) as Array<keyof typeof MyEnum>).map(
  (key) => {
    //...
  }
);

to call Object.keys with MyEnum to return an array of enum keys.

Then we call map with a callback to return an array with the mapped entries.

Conclusion

To map Typescript enums, we get the keys and map them.