Categories
JavaScript Answers

How to print a message in error handling with try-catch with JavaScript?

Sometimes, we want to print a message in error handling with try-catch with JavaScript.

In this article, we’ll look at how to print a message in error handling with try-catch with JavaScript.

How to print a message in error handling with try-catch with JavaScript?

To print a message in error handling with try-catch with JavaScript, we can log the stack, name and message properties.

For instance, we write:

const x = {
  asd: "asd",
};

try {
  JSON.parse(x);
} catch (e) {
  console.log("Error", e.stack);
  console.log("Error", e.name);
  console.log("Error", e.message);
}

The stack property has the stack trace string.

name has the error name as a string.

And message has the error message as a string.

Therefore, we should see 'Error SyntaxError' and 'Error Unexpected token o in JSON at position 1' logged respectively for e.name and e.message.

Conclusion

To print a message in error handling with try-catch with JavaScript, we can log the stack, name and message properties.

Categories
JavaScript Answers

How to call JavaScript function from Handlebars?

Sometimes, we want to call JavaScript function from Handlebars.

In this article, we’ll look at how to call JavaScript function from Handlebars.

How to call JavaScript function from Handlebars?

To call JavaScript function from Handlebars, we can register a helper.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.7/handlebars.min.js"></script>

<script id="some-template" type="text/x-handlebars-template">
  {{printItems items}}
</script>

<div>

</div>

to add a template that uses the printItems helper.

Then we create the printItems helper and use it by writing:

Handlebars.registerHelper("printItems", (items) => {
  return items.join(', ');
});

const source = $("#some-template").html();
const template = Handlebars.compile(source);
const data = {
  items: ['foo', 'bar']
}
$('div').append(template(data));

We call Handlebars.registerHelper method with the helper name and the helper function.

It accepts the value after the helper as the argument.

We have items in data, so the values in items will be printed.

We return a string with the items entries joined together into a string.

Then we get the template and render it with:

const source = $("#some-template").html();
const template = Handlebars.compile(source);
const data = {
  items: ['foo', 'bar']
}
$('div').append(template(data));

Therefore, we see ‘foo, bar’ printed.

Conclusion

To call JavaScript function from Handlebars, we can register a helper.

Categories
JavaScript Answers

How to set the date of an existing moment.js object with JavaScript?

Sometimes, we want to set the date of an existing moment.js object with JavaScript.

In this article, we’ll look at how to set the date of an existing moment.js object with JavaScript.

How to set the date of an existing moment.js object with JavaScript?

To set the date of an existing moment.js object with JavaScript, we can use the set method.

For instance, we write:

const m = moment();
const myDate = new Date();
const newDate = moment(myDate);
m.set(newDate.toObject());
console.log(m)

We call moment to create a moment object.

Then we create the newDate moment object with the current date and time.

Next, we call newDate.toObject to return the date and time data in an object.

Finally, we call m.set with the returned object to set the new date and time to the same date and time as newDate.

Conclusion

To set the date of an existing moment.js object with JavaScript, we can use the set method.

Categories
JavaScript Answers

How to find out where a function is called from in JavaScript?

Sometimes, we want to find out where a function is called from in JavaScript.

In this article, we’ll look at how to find out where a function is called from in JavaScript.

How to find out where a function is called from in JavaScript?

To find out where a function is called from in JavaScript, we can use the console.trace method.

For instance, we write:

const foo = () => {}
console.trace();
foo()

Then when console.trace is called, the console.trace method will log the stack trace onto the console.

Conclusion

To find out where a function is called from in JavaScript, we can use the console.trace method.

Categories
JavaScript Answers

How to paste rich text from clipboard to HTML text area element?

Sometimes, we want to paste rich text from clipboard to HTML text area element.

In this article, we’ll look at how to paste rich text from clipboard to HTML text area element.

How to paste rich text from clipboard to HTML text area element?

To paste rich text from clipboard to HTML text area element, we can listen for the paste event and put the pasted content in a contenteditable element.

For instance, we write:

<div id="target" contenteditable></div>

to add a contenteditable div.

Then we write:

document.addEventListener('paste', (e) => {
  e.preventDefault();
  let pastedText = ''
  if (e.clipboardData && e.clipboardData.getData) {
    pastedText = e.clipboardData.getData('text/html');
  }
  document.getElementById('target').innerHTML = pastedText
});

We call document.addEventListener with 'paste' to listen for the paste event on the browser window.

We call it with a callback that calls e.preventDefault to prevent the default behavior,

And then we call e.clipboardData.getData to get the pasted data.

And we set the HTML content of the contenteditable div with:

document.getElementById('target').innerHTML = pastedText

As a result, we see the pasted text has the styles preserved.

Conclusion

To paste rich text from clipboard to HTML text area element, we can listen for the paste event and put the pasted content in a contenteditable element.