Categories
JavaScript Answers

How to get the value from a table cell in JavaScript?

Sometimes, we want to get the value from a table cell in JavaScript.

In this article, we’ll look at how to get the value from a table cell in JavaScript.

How to get the value from a table cell in JavaScript?

To get the value from a table cell in JavaScript, we loop through the table rows and columns.

For instance, we write

const getCellValues = () => {
  const table = document.getElementById("mytable");
  for (const row of table.rows) {
    for (const cell of row.cells) {
      console.log(cell.innerHTML);
    }
  }
};

to define the getCellValues function.

In it, we get the table element with getElementById.

Next, we use a for-of loop to loop through the table.rows table rows.

In it, we loop through the cells in row.cells.

Then we get the cell’s value with the cell.innerHTML property.

Conclusion

To get the value from a table cell in JavaScript, we loop through the table rows and columns.

Categories
JavaScript Answers

How to match special characters and letters in regex in JavaScript?

Sometimes, we want to match special characters and letters in regex in JavaScript.

In this article, we’ll look at how to match special characters and letters in regex in JavaScript.

How to match special characters and letters in regex in JavaScript?

To match special characters and letters in regex in JavaScript, we create a regex that matches the special characters we want.

For instance, we write

const pattern = /^[\w&.-]+$/;

if (pattern.test(qry)) {
  // ...
}

to define the pattern regex.

In it, we put the special characters we want to match in the square brackets.

Then we call pattern.test to check if the qry string matches the pattern.

Conclusion

To match special characters and letters in regex in JavaScript, we create a regex that matches the special characters we want.

Categories
JavaScript Answers

How to open URL by clicking on marker with Google Maps API with JavaScript?

Sometimes, we want to open URL by clicking on marker with Google Maps API with JavaScript.

In this article, we’ll look at how to open URL by clicking on marker with Google Maps API with JavaScript.

How to open URL by clicking on marker with Google Maps API with JavaScript?

To open URL by clicking on marker with Google Maps API with JavaScript, we add listeners to each marker.

For instance, we write

google.maps.event.addListener(
  marker,
  "click",
  ((marker, i) => {
    return () => {
      window.location.href = marker.url;
    };
  })(marker, i)
);

to call addEventListener to add a click listener to the marker.

We use the function returned by the outer function as the listener.

And we go to the url of the marker when we click on it.

Conclusion

To open URL by clicking on marker with Google Maps API with JavaScript, we add listeners to each marker.

Categories
JavaScript Answers

How to toggle visibility property of a div with JavaScript?

Sometimes, we want to toggle visibility property of a div with JavaScript.

In this article, we’ll look at how to toggle visibility property of a div with JavaScript.

How to toggle visibility property of a div with JavaScript?

To toggle visibility property of a div with JavaScript, we set the style.visibility property.

For instance, we write

const toggleVideo = () => {
  const e = document.getElementById("video-over");
  if (e.style.visibility === "visible") {
    e.style.visibility = "hidden";
  } else if (e.style.visibility === "hidden") {
    e.style.visibility = "visible";
  }
};

to define the toggleVideo function.

In it, we get the element with getElementById.

Then we check if it’s visible with e.style.visibility === "visible".

If it’s true, we set it toi 'hidden'.

Otherwise, we set it to 'visible'.

Conclusion

To toggle visibility property of a div with JavaScript, we set the style.visibility property.

Categories
JavaScript Answers

How to get element by a custom attribute using JavaScript?

Sometimes, we want to get element by a custom attribute using JavaScript.

In this article, we’ll look at how to get element by a custom attribute using JavaScript.

How to get element by a custom attribute using JavaScript?

To get element by a custom attribute using JavaScript, we use querySelector.

For instance, we write

const element = document.querySelector('[tokenid="14"]');

to get the element with tokenid attribute set to 14 with querySelector.

Conclusion

To get element by a custom attribute using JavaScript, we use querySelector.