Categories
JavaScript Answers

How to create table using JavaScript?

Sometimes, we want to create table using JavaScript.

In this article, we’ll look at how to create table using JavaScript.

How to create table using JavaScript?

To create table using JavaScript, we can use the insertRow and insertCell methods.

For instance, we write

const createTable = (row, col) => {
  let body = document.body;
  let tbl = document.createElement("table");

  for (let i = 0; i < row; i++) {
    let tr = tbl.insertRow();
    for (let j = 0; j < col; j++) {
      let td = tr.insertCell();
      td.appendChild(document.createTextNode(`${i},${j}`));
      td.style.border = "1px solid black";
    }
  }
  body.appendChild(tbl);
};

createTable(5, 5);

to create the table element with createElement.

Then we create a nested for loop to create the rows and cells and put them in the table.

We call tbl.insertRow to insert a tr element.

And then we call tr.insertCell to insert a td element into the tr element.

Then we call createTextNode to create a text node and call td.appendChild to append the text node as its child.

Finally, we call body.appendChild to append the table as the child of the body.

Conclusion

To create table using JavaScript, we can use the insertRow and insertCell methods.

Categories
JavaScript Answers

How to convert string to number in JavaScript?

Sometimes, we want to convert string to number in JavaScript.

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

How to convert string to number in JavaScript?

To convert string to number in JavaScript, we can use the Number, parseInt, parseFloat functions, or the unary + operator.

For instance, we write

const num = Number(x);

or

const num = parseInt(x, 10);

or

const num = parseFloat(x);

or

const num = +x;

to convert string x to a number with the Number, parseInt, parseFloat functions, or the unary + operator.

Unary +, Number and parseFloat convert x to an floating point number.

And parseInt converts x to an integer.

parseInt takes the radix as a 2nd argument. 10 means we parse x into a decimal number.

Conclusion

To convert string to number in JavaScript, we can use the Number, parseInt, parseFloat functions, or the unary + operator

Categories
JavaScript Answers

How to remove disabled attribute from HTML input with JavaScript?

Sometimes, we want to remove disabled attribute from HTML input with JavaScript.

In this article, we’ll look at how to remove disabled attribute from HTML input with JavaScript.

How to remove disabled attribute from HTML input with JavaScript?

To remove disabled attribute from HTML input with JavaScript, we set the disabled property to false.

For instance, we write

document.getElementById("my-input-id").disabled = false;

to select the input with getElementById.

Then we set its disabled property to false to remove the disabled attribute from the input.

Conclusion

To remove disabled attribute from HTML input with JavaScript, we set the disabled property to false.

Categories
JavaScript Answers

How to fix select not rendering with Materialize CSS?

Sometimes, we want to fix select not rendering with Materialize CSS.

In this article, we’ll look at how to fix select not rendering with Materialize CSS.

How to fix select not rendering with Materialize CSS?

To fix select not rendering with Materialize CSS, we wait for the DOM to be loaded completely before we apply the styles for the select drop downs.

For instance, we write

document.addEventListener("DOMContentLoaded", () => {
  const elems = document.querySelectorAll("select");
  const instances = M.FormSelect.init(elems);
});

to listen for the DOMContentLoaded event to see when the DOM finishes loading.

The callback runs when the DOM finishes loading.

In it, we select all the select elements with querySelectorAll.

And then we call M.FormSelect.init on the elems node list which has the select elements.

Conclusion

To fix select not rendering with Materialize CSS, we wait for the DOM to be loaded completely before we apply the styles for the select drop downs.

Categories
JavaScript Answers

How to check with JavaScript if connection is local host?

Sometimes, we want to check with JavaScript if connection is local host.

In this article, we’ll look at how to check with JavaScript if connection is local host.

How to check with JavaScript if connection is local host?

To check with JavaScript if connection is local host, we can check the value of location.hostname.

For instance, we write

if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
  alert("It's a local server!");
}

to check if location.hostname is 'localhost' or '127.0.0.1'.

If either of these are true, then we’re running our app on localhost.

Conclusion

To check with JavaScript if connection is local host, we can check the value of location.hostname.