Categories
JavaScript Answers

How to prevent contenteditable element from adding div on pressing enter with Chrome and JavaScript?

Sometimes, we want to prevent contenteditable element from adding div on pressing enter with Chrome and JavaScript.

In this article, we’ll look at how to prevent contenteditable element from adding div on pressing enter with Chrome and JavaScript.

How to prevent contenteditable element from adding div on pressing enter with Chrome and JavaScript?

To prevent contenteditable element from adding div on pressing enter with Chrome and JavaScript, we can listen for the keydown event on the contenteditable element and prevent the default behavior when Enter is pressed.

For instance, we write

<div class="element" contenteditable="true">
  Sample text
</div>

to add a contenteditable div.

Then we write

document.addEventListener("keydown", (event) => {
  if (event.key === "Enter") {
    document.execCommand("insertLineBreak");
    event.preventDefault();
  }
});

to listen to the document’s keydown event with addEventListener.

In the event listener callback, we check if the key pressed is Enter with

event.key === "Enter"

If it is, we run document.execCommand("insertLineBreak") to insert a line break.

And we call event.preventDefault to prevent the div from being inserted.

Conclusion

To prevent contenteditable element from adding div on pressing enter with Chrome and JavaScript, we can listen for the keydown event on the contenteditable element and prevent the default behavior when Enter is pressed.

Categories
JavaScript Answers

How to convert JavaScript array to string?

Sometimes, we want to convert JavaScript array to string.

In this article, we’ll look at how to convert JavaScript array to string.

How to convert JavaScript array to string?

To convert JavaScript array to string, we can use the array join method.

For instance, we write

const arr = ["A", "B", "C"];
const arrString = arr.join(", ");

to call arr.join to combine the strings in arr with each entry separated by commas.

Conclusion

To convert JavaScript array to string, we can use the array join method.

Categories
JavaScript Answers

How to wait for a JavaScript Promise to resolve before resuming function?

Sometimes, we want to wait for a JavaScript Promise to resolve before resuming function.

In this article, we’ll look at how to wait for a JavaScript Promise to resolve before resuming function.

How to wait for a JavaScript Promise to resolve before resuming function?

To wait for a JavaScript Promise to resolve before resuming function, we use async and await.

For instance, we write

const wrapperFunc = async () => {
  try {
    let r1 = await someFunc();
    let r2 = await someFunc2(r1);
    return someValue;
  } catch (e) {
    console.log(e);
    throw e;
  }
};

to call someFunc and someFunc2 which return promises.

We use await to wait for the returned promises to finish in each function and then get their resolved value.

If any returned promise is rejected, then the catch block runs and we get the rejected reason from the e parameter.

We can only use await inside async functions and async functions only return promises.

Conclusion

To wait for a JavaScript Promise to resolve before resuming function, we use async and await.

Categories
JavaScript Answers

How to set boolean values in LocalStorage with JavaScript?

To set boolean values in LocalStorage with JavaScript, we can parse the string value into a boolean with JSON.parse.

For instance, we write

const value = "true";
console.log(JSON.parse(value) === true);

to parse 'true' back into true with JSON.parse.

Then the console log should log true since the left side returns true.

Categories
JavaScript Answers

How to unpack array into separate variables in JavaScript?

Sometimes, we want to unpack array into separate variables in JavaScript.

In this article, we’ll look at how to unpack array into separate variables in JavaScript.

How to unpack array into separate variables in JavaScript?

To unpack array into separate variables in JavaScript, we use the array destructuring syntax.

For instance, we write

const [x, y] = ["foo", "bar"];
console.log(x);
console.log(y);

to assign 'foo' to x and 'bar' to y.

Conclusion

To unpack array into separate variables in JavaScript, we use the array destructuring syntax.