Categories
JavaScript Answers

How to add JavaScript custom Event Listener?

Sometimes, we want to add JavaScript custom Event Listener.

In this article, we’ll look at how to add JavaScript custom Event Listener.

How to add JavaScript custom Event Listener?

To add JavaScript custom Event Listener, we call addEventListener.

For instance, we write

const evt = document.createEvent("Event");
evt.initEvent("myEvent", true, true);
evt.foo = "bar";

document.addEventListener("myEvent", myEventHandler, false);

document.dispatchEvent(evt);

to add a listener for myEvent triggered by document.

We create the event with createEvent.

Then we call initEvent to set the event name.

And we add some properties to it with

evt.foo = "bar";

Then we call dispatchEvent to trigger the event.

Conclusion

To add JavaScript custom Event Listener, we call addEventListener.

Categories
Vue Answers

How to add a Google Font to a Vue.js Component?

Sometimes, we want to add a Google Font to a Vue.js Component.

In this article, we’ll look at how to add a Google Font to a Vue.js Component.

How to add a Google Font to a Vue.js Component?

To add a Google Font to a Vue.js Component, we use the @import directive.

For instance, we write

<style>
@import url("https://fonts.googleapis.com/css?family=Roboto+Condensed");

html,
body {
  font-family: "Roboto", sans-serif;
}

#app {
  font-family: "Roboto", sans-serif;
}
</style>

to import the Roboto Google font with @import.

Then we can set font-family to that to use it.

Conclusion

To add a Google Font to a Vue.js Component, we use the @import directive.

Categories
JavaScript Answers

How to generate HTML table from a 2D JavaScript array?

Sometimes, we want to generate HTML table from a 2D JavaScript array.

In this article, we’ll look at how to generate HTML table from a 2D JavaScript array.

How to generate HTML table from a 2D JavaScript array?

To generate HTML table from a 2D JavaScript array, we can use the insertRow and insertCell methods.

For instance, we write

const createTable = (tableData) => {
  const table = document.createElement("table");
  let row = {};
  let cell = {};

  tableData.forEach((rowData) => {
    row = table.insertRow(-1);
    rowData.forEach((cellData) => {
      cell = row.insertCell();
      cell.textContent = cellData;
    });
  });
  document.body.appendChild(table);
};

to create a table with createElement.

Then we loop through the tableData array with forEach.

In it the callback, we call insertRow to insert a tr element.

Then we loop through the rowData with forEach and call insertCell to insert a td.

Then we set its textContent to cellData.

Finally, we call appendChild to append the table to the body element.

Conclusion

To generate HTML table from a 2D JavaScript array, we can use the insertRow and insertCell methods.

Categories
JavaScript Answers

How to update and delete a cookie with JavaScript?

To update and delete a cookie with JavaScript, we set the document.cookie string.

For instance, we write

document.cookie = "username=Arnold";

to set document.cookie to the cookie string with the key and value.

We remove the cookie by adding the expires key and setting its value to a date in the past.

Conclusion

To update and delete a cookie with JavaScript, we set the document.cookie string.

Categories
JavaScript Answers

How to get the text area value of a CKEditor text area with JavaScript?

Sometimes, we want to get the text area value of a CKEditor text area with JavaScript.

In this article, we’ll look at how to get the text area value of a CKEditor text area with JavaScript.

How to get the text area value of a CKEditor text area with JavaScript?

To get the text area value of a CKEditor text area with JavaScript, we use the getData method.

For instance, we write

<textarea id="editor1" name="editor1">This is sample text</textarea>

to add a text area.

Then we write

const editorText = CKEDITOR.instances.editor1.getData();

to call getData to get the text area’s value.

Conclusion

To get the text area value of a CKEditor text area with JavaScript, we use the getData method.