Categories
JavaScript Answers

How to insert a row in an HTML table body in JavaScript?

Sometimes, we want to insert a row in an HTML table body in JavaScript.

In this article, we’ll look at how to insert a row in an HTML table body in JavaScript.

How to insert a row in an HTML table body in JavaScript?

To insert a row in an HTML table body in JavaScript, we can use the insertRow and insertCell methods.

For instance, we write

const tbodyRef = document
  .getElementById("myTable")
  .getElementsByTagName("tbody")[0];
const newRow = tbodyRef.insertRow();
const newCell = newRow.insertCell();
const newText = document.createTextNode("new row");
newCell.appendChild(newText);

to get the tbody element with

const tbodyRef = document
  .getElementById("myTable")
  .getElementsByTagName("tbody")[0];

Then we call insertRow to insert a tr element into the tbody element.

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

Then we call createTextNode to create a text node.

Finally, we use

newCell.appendChild(newText);

to insert the text node into the td element.

Conclusion

To insert a row in an HTML table body in JavaScript, we can use the insertRow and insertCell methods.

Categories
JavaScript Answers

How to check if a background image is loaded with JavaScript?

Sometimes, we want to check if a background image is loaded with JavaScript.

In this article, we’ll look at how to check if a background image is loaded with JavaScript.

How to check if a background image is loaded with JavaScript?

To check if a background image is loaded with JavaScript, we can set the backgroundImage property.

For instance, we write

const src = "https://picsum.photos/200/300";
const image = new Image();
image.addEventListener("load", () => {
  body.style.backgroundImage = `url(${src})`;
});
image.src = src;

to create the img element with Image.

Then we listen for the load event on the image to check if it’s loaded.

If it is, then we set the backgroundImage to the URL of the loaded image.

Then we load the image by setting image.src to src.

Conclusion

To check if a background image is loaded with JavaScript, we can set the backgroundImage property.

Categories
JavaScript Answers

How to call a JavaScript function named in a variable?

Sometimes, we want to call a JavaScript function named in a variable.

In this article, we’ll look at how to call a JavaScript function named in a variable.

How to call a JavaScript function named in a variable?

To call a JavaScript function named in a variable, we can get the function by the string key.

For instance, we write

function foo() {
  alert("foo");
}

const a = "foo";
window[a]();

to get the foo global function with window[a], which evaluates to window.foo.

And then we call that.

Conclusion

To call a JavaScript function named in a variable, we can get the function by the string key.

Categories
JavaScript Answers

How to check if anonymous object has a method with JavaScript?

Sometimes, we want to check if anonymous object has a method with JavaScript.

In this article, we’ll look at how to check if anonymous object has a method with JavaScript.

How to check if anonymous object has a method with JavaScript?

To check if anonymous object has a method with JavaScript, we can check with the typeof operator.

For instance, we write

const myObj = {
  prop1: "no",
  prop2() {
    return false;
  },
};

if (typeof myObj.prop2 === "function") {
  console.log("It's a function");
} else if (typeof myObj.prop2 === "undefined") {
  console.log("It's undefined");
} else {
  console.log("It's neither undefined nor a function");
}

to define the myObj object.

Then we check if myObj.prop2 is a function with the typeof operator.

To check if it’s a function, we write

typeof myObj.prop2 === "function"

to check if prop2 is a function.

Conclusion

To check if anonymous object has a method with JavaScript, we can check with the typeof operator.

Categories
JavaScript Answers

How to change the selected option of an HTML select element with JavaScript?

Sometimes, we want to change the selected option of an HTML select element with JavaScript.

In this article, we’ll look at how to change the selected option of an HTML select element with JavaScript.

How to change the selected option of an HTML select element with JavaScript?

To change the selected option of an HTML select element with JavaScript, we can set the value property of the select element.

For instance, we write

<select id="sel">
  <option value="1">Cat</option>
  <option value="2">Dog</option>
  <option value="3">Fish</option>
</select>

to add the select element.

Then we write

document.getElementById("sel").value = "2";

to set the selection option to the 2nd option by selecting the select drop down with getElementById and setting its value to `’2′.

Conclusion

To change the selected option of an HTML select element with JavaScript, we can set the value property of the select element.