Categories
JavaScript Answers

How to add onClick event handler to document.createElement(“th”) with JavaScript?

Sometimes, we want to add onClick event handler to document.createElement("th") with JavaScript.

In this article, we’ll look at how to add onClick event handler to document.createElement("th") with JavaScript.

How to add onClick event handler to document.createElement("th") with JavaScript?

To add onClick event handler to document.createElement("th") with JavaScript, we set the onclick property of the th element.

For instance, we write

const newTH = document.createElement("th");
newTH.onclick = () => {
  //...
};

to call createElement with 'th' to create a th element.

Then we set its onclick property to a function that’s called when we click on the th element.

Conclusion

To add onClick event handler to document.createElement("th") with JavaScript, we set the onclick property of the th element.

Categories
JavaScript Answers

How to fix JavaScript ES6 TypeError: Class constructor Client cannot be invoked without ‘new’?

Sometimes, we want to fix JavaScript ES6 TypeError: Class constructor Client cannot be invoked without ‘new’.

In this article, we’ll look at how to fix JavaScript ES6 TypeError: Class constructor Client cannot be invoked without ‘new’.

How to fix JavaScript ES6 TypeError: Class constructor Client cannot be invoked without ‘new’?

To fix JavaScript ES6 TypeError: Class constructor Client cannot be invoked without ‘new’, we need to transpile our code to ES6 or later.

In tsconfig.json, we write

{
  "compilerOptions": {
    "target": "ES6"
    //...
  }
}

to set the target to 'ES6' to transpile the code to ES6 so ES6 classes are kept after transpiling.

Conclusion

To fix JavaScript ES6 TypeError: Class constructor Client cannot be invoked without ‘new’, we need to transpile our code to ES6 or later.

Categories
JavaScript Answers

How to defer CSS loading with JavaScript?

Sometimes, we want to defer CSS loading with JavaScript.

In this article, we’ll look at how to defer CSS loading with JavaScript.

How to defer CSS loading with JavaScript?

To defer CSS loading with JavaScript, we can create a link element with JavaScript.

For instance, we write

const stylesheet = document.createElement("link");
stylesheet.href = "style.css";
stylesheet.rel = "stylesheet";
stylesheet.type = "text/css";
document.head.appendChild(stylesheet);

to create a link element with createElement.

Then we set the href, rel and type properties to set the attributes with the same name.

Then we call document.head.appendChild with stylesheet to append the link element as the last child of the head element.

Conclusion

To defer CSS loading with JavaScript, we can create a link element with JavaScript.

Categories
JavaScript Answers

How to pass an object to a JavaScript function?

Sometimes, we want to pass an object to a JavaScript function.

In this article, we’ll look at how to pass an object to a JavaScript function.

How to pass an object to a JavaScript function?

To pass an object to a JavaScript function, we can add a parameter that accepts an object.

For instance, we write

const someFunc = (arg) => {
  alert(arg.foo);
  alert(arg.bar);
};

someFunc({ foo: "This", bar: "works!" });

to define the someFunc function that takes the arg object argument.

Then we get the properties with arg.foo and arg.bar.

Next we call someFunc with an object with the properties.

Conclusion

To pass an object to a JavaScript function, we can add a parameter that accepts an object.

Categories
JavaScript Answers

How to convert string with dot or comma as decimal separator to number in JavaScript?

Sometimes, we want to convert string with dot or comma as decimal separator to number in JavaScript.

In this article, we’ll look at how to convert string with dot or comma as decimal separator to number in JavaScript.

How to convert string with dot or comma as decimal separator to number in JavaScript?

To convert string with dot or comma as decimal separator to number in JavaScript, we use the string replace method.

For instance, we write

const num = parseFloat(str.replace(",", ".").replace(" ", ""));

to call replace to replace commas with periods and spaces with with empty strings.

Then we call parseFloat to convert the string into a decimal number and return it.

Conclusion

To convert string with dot or comma as decimal separator to number in JavaScript, we use the string replace method.