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

Categories
JavaScript Answers

How to trigger CSS animations in JavaScript?

Sometimes, we want to trigger CSS animations in JavaScript.

In this article, we’ll look at how to trigger CSS animations in JavaScript.

How to trigger CSS animations in JavaScript?

To trigger CSS animations in JavaScript, we add keyframes for our animation.

For instance, we write

@keyframes spin1 {
  100% {
    transform: rotate(360deg);
  }
}
@keyframes spin2 {
  100% {
    transform: rotate(-360deg);
  }
}
@keyframes idle {
  100% {
  }
}

to add keyframes for the spin1, spin2, and idle animations.

Then we make our element animate by writing

document.getElementById("yourElement").style.animation =
  "spin2 4s linear infinite";

We select the element with getElementById.

Then we set its style.animation property to a string that has the animation name.

Conclusion

To trigger CSS animations in JavaScript, we add keyframes for our animation.