Categories
JavaScript Answers

How to detect when a user leaves a website with JavaScript?

Sometimes, we want to detect when a user leaves a website with JavaScript.

In this article, we’ll look at how to detect when a user leaves a website with JavaScript.

How to detect when a user leaves a website with JavaScript?

To detect when a user leaves a website with JavaScript, we can add an event listener for the unload event.

For instance, we write:

window.onunload = () => {
  console.log('unload')
};

to set window.onunload to a function that runs when the page is being unloaded.

Therefore, when we leave the page, we’ll see 'unload' logged.

Conclusion

To detect when a user leaves a website with JavaScript, we can add an event listener for the unload event.

Categories
JavaScript Answers

How to create a mouseover effect with JavaScript addEventListener?

Sometimes, we want to create a mouseover effect with JavaScript addEventListener.

In this article, we’ll look at how to create a mouseover effect with JavaScript addEventListener.

How to create a mouseover effect with JavaScript addEventListener?

To create a mouseover effect with JavaScript addEventListener, we can pass in a callback to set the styles to create the mouseover effect.

For instance, we write:

<button>
  click me
</button>

to add a button.

Then we write:

const button = document.querySelector("button");

button.addEventListener("mouseover", () => {
  button.setAttribute("style", "background-color: blue;")
});
button.addEventListener("mouseout", () => {
  button.setAttribute("style", "background-color: red;")
});

to select the button with querySelector.

Then we call button.addEventListener to add the mouseover and mouseout event listeners.

The callback sets the background of the button to blue when we hover over the button and sets it to red when we move our mouse away from it.

Conclusion

To create a mouseover effect with JavaScript addEventListener, we can pass in a callback to set the styles to create the mouseover effect.

Categories
JavaScript Answers

How to add ripple effect when clicking a card in Material UI and JavaScript?

Sometimes, we want to add ripple effect when clicking a card in Material UI and JavaScript.

In this article, we’ll look at how to add ripple effect when clicking a card in Material UI and JavaScript.

How to add ripple effect when clicking a card in Material UI and JavaScript?

To add ripple effect when clicking a card in Material UI and JavaScript, we can add a card with the Card component.

For instance, we write:

import React from "react";
import Card from "@material-ui/core/Card";
import CardActionArea from "@material-ui/core/CardActionArea";
import CardContent from "@material-ui/core/CardContent";

export default function App() {
  return (
    <div>
      <Card>
        <CardActionArea>
          <CardContent>hello world</CardContent>
        </CardActionArea>
      </Card>
    </div>
  );
}

We add the Card, CardActionArea and CardContent components to add a card onto the page.

As a result, when we hover over the card, we should see the ripple effect displaying.

Conclusion

To add ripple effect when clicking a card in Material UI and JavaScript, we can add a card with the Card component.

Categories
JavaScript Answers

How to create a simple table with React and JavaScript?

Sometimes, we want to create a simple table with React and JavaScript.

In this article, we’ll look at how to create a simple table with React and JavaScript.

How to create a simple table with React and JavaScript?

To create a simple table with React and JavaScript, we can use the array map method.

For instance, we write:

import React from "react";

const arr = [
  { firstName: "john", lastName: "smith" },
  { firstName: "jane", lastName: "doe" },
  { firstName: "mary", lastName: "smith" }
];

export default function App() {
  const rows = arr.map((a) => {
    const { firstName, lastName } = a;
    return (
      <tr>
        <td>{firstName}</td>
        <td>{lastName}</td>
      </tr>
    );
  });

  return (
    <div>
      <table>
        <tbody>{rows}</tbody>
      </table>
    </div>
  );
}

We call arr.map with a callback that returns the table rows which we create by populating it with the arr entry’s values.

Then we put the row in between the tbody tags to add the rows as the children of tbody.

Therefore, we see:

john	smith
jane	doe
mary	smith

displayed.

Conclusion

To create a simple table with React and JavaScript, we can use the array map method.

Categories
JavaScript Answers

How to match 6 digit hexadecimal numbers with JavaScript regular expressions?

Sometimes, we want to match 6 digit hexadecimal numbers with JavaScript regular expressions.

In this article, we’ll look at how to match 6 digit hexadecimal numbers with JavaScript regular expressions.

How to match 6 digit hexadecimal numbers with JavaScript regular expressions?

To match 6 digit hexadecimal numbers with JavaScript regular expressions, we can use the /[0-9A-Fa-f]{6}/g regex.

For instance, we write:

console.log(/[0-9A-Fa-f]{6}/g.test('ffffff'))
console.log(/[0-9A-Fa-f]{6}/g.test('abc'))

to check if 'ffffff' and 'abc' are valid 6 digit hex numbers with the regex test method.

Since 'ffffff' is a valid 6 digit hex number, the first log logs true.

And since 'abc' isn’t a valid 6 digit hex number, the first log logs false.

Conclusion

To match 6 digit hexadecimal numbers with JavaScript regular expressions, we can use the /[0-9A-Fa-f]{6}/g regex.