Categories
JavaScript Answers

How to check if mouse is inside div with JavaScript?

Sometimes, we want to check if mouse is inside div with JavaScript.

In this article, we’ll look at how to check if mouse is inside div with JavaScript.

How to check if mouse is inside div with JavaScript?

To check if mouse is inside div with JavaScript, we can listen to the mouseenter and mouseout events triggered by the div.

For instance, we write:

<div>
  hello world
</div>

to add a div.

Then we write:

const div = document.querySelector("div")
let isOnDiv = false;
div.onmouseenter = () => {
  isOnDiv = true
  console.log(isOnDiv)
}
div.onmouseout = () => {
  isOnDiv = false
  console.log(isOnDiv)
}

to select the div with querySelector.

Next, we set div.onmouseenter to a function that sets isOnDiv to true.

And we set div.onmouseout to a function that sets isOnDiv to false.

Now when our mouse pointer is in the div, true is logged.

And when our mouse pointer leaves the div, false is logged.

Categories
JavaScript Answers

How to get parent element and write holder div for siblings with JavaScript?

Sometimes, we want to get parent element and write holder div for siblings with JavaScript.

In this article, we’ll look at how to get parent element and write holder div for siblings with JavaScript.

How to get parent element and write holder div for siblings with JavaScript?

To get parent element and write holder div for siblings with JavaScript, we can use the parentNode property to get the parent node.

For instance, we write:

<div>
  <div id='child1'>
    hello world
  </div>
</div>

to add nested divs.

Then we write:

const child1 = document.getElementById("child1")
const parent = child1.parentNode
const contents = parent.innerHTML;
parent.innerHTML = `<div id="holder">${contents}</div>`;

to get the div with ID child1 with getElementById.

Then we get its parent node with parentNode.

Next, we get the parent node’s content with innerHTML.

And then we set parent.innerHTML to a string with a div wrapped around the contents.

Now the div with ID holder is wrapped around the div with ID child1.

Conclusion

To get parent element and write holder div for siblings with JavaScript, we can use the parentNode property to get the parent node.

Categories
JavaScript Answers

How to get the X and Y coordinates for a div element with JavaScript?

Sometimes, we want to get the X and Y coordinates for a div element with JavaScript.

In this article, we’ll look at how to get the X and Y coordinates for a div element with JavaScript.

How to get the X and Y coordinates for a div element with JavaScript?

To get the X and Y coordinates for a div element with JavaScript, we an use the getBoundingXClientRect method.

For instance, we add a div by writing:

<div>
  hello world
</div>

Then we write:

const element = document.querySelector('div');
const position = element.getBoundingClientRect();
const x = position.left;
const y = position.top;
console.log(x, y)

to select the div with querySelector.

And then we call getBoundingClientRect to get the position of the div.

Then we get the x and y coordinates of the div with position.left and position.top.

Conclusion

To get the X and Y coordinates for a div element with JavaScript, we an use the getBoundingXClientRect method.

Categories
JavaScript Answers

How to get the current date minus 20 seconds in JavaScript?

Sometimes, we want to get the current date minus 20 seconds in JavaScript.

In this article, we’ll look at how to get the current date minus 20 seconds in JavaScript.

How to get the current date minus 20 seconds in JavaScript?

To get the current date minus 20 seconds in JavaScript, we can get the timestamp of the date and then we subtract 20000 milliseconds from the timestamp.

For instance, we write:

const d = new Date(2022, 1, 1)
const newD = new Date(d.getTime() - 20000);
console.log(newD)

to create date d.

Then we call d.getTime to return the timestamp of the date in milliseconds.

Next, we subtract 20000 milliseconds from the timestamp to get the timestamp of d minus 20 seconds.

And then we use the returned timestamp to create a new date with the Date constructor.

Therefore, newD is Mon Jan 31 2022 23:59:40 GMT-0800 (Pacific Standard Time) .

Conclusion

To get the current date minus 20 seconds in JavaScript, we can get the timestamp of the date and then we subtract 20000 milliseconds from the timestamp.

Categories
JavaScript Answers

How to select elements and add class to them in JavaScript?

Sometimes, we want to select elements and add class to them in JavaScript.

In this article, we’ll look at how to select elements and add class to them in JavaScript.

How to select elements and add class to them in JavaScript?

To select elements and add class to them in JavaScript, we can use querySelectorAll and a for-of loop.

For instance, we write:

<ul>
  <li class="even"></li>
  <li class="odd"></li>
  <li class="even"></li>
</ul>

to add a list.

Then we select all the li’s and add a class to them by writing:

const list = document.querySelectorAll("li.even, li.odd");
for (const li of list) {
  li.classList.add('cf');
}

We select all the li’s with querySelectorAll.

And then we loop through them with a for-of loop.

In the loop, we call li.classList.add to add the 'cf' class to each li element.

Conclusion

To select elements and add class to them in JavaScript, we can use querySelectorAll and a for-of loop.