Categories
JavaScript Answers

How to detect Android phone via JavaScript?

Sometimes, we want to detect Android phone via JavaScript.

In this article, we’ll look at how to detect Android phone via JavaScript.

How to detect Android phone via JavaScript?

To detect Android phone via JavaScript, we can check if the user agent has 'android' in it.

For instance, we write

const ua = navigator.userAgent.toLowerCase();
const isAndroid = ua.indexOf("android") > -1;
if (isAndroid) {
  //...
}

to get the user agent with navigator.userAgent.

Then we convert it to lower case with toLowerCase.

Next we check if 'android' is in the user agent string with indexof.

Conclusion

To detect Android phone via JavaScript, we can check if the user agent has 'android' in it.

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.