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
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.

Categories
JavaScript Answers

How to round numbers with JavaScript parseFloat?

Sometimes, we want to round numbers with JavaScript parseFloat.

In this article, we’ll look at how to round numbers with JavaScript parseFloat.

How to round numbers with JavaScript parseFloat?

To round numbers with JavaScript parseFloat, we use the toFixed method.

For instance, we write

const rounded = parseFloat(num.toFixed(2));

to call toFixed on the num number with 2 to return a number string rounded to 2 decimal places.

Then we call parseFloat to convert the string to a number rounded to 2 decimal places.

Conclusion

To round numbers with JavaScript parseFloat, we use the toFixed method.