Categories
JavaScript Answers

How to remove text without removing inner elements from a parent element using JavaScript?

Sometimes, we want to remove text without removing inner elements from a parent element using JavaScript.

In this article, we’ll look at how to remove text without removing inner elements from a parent element using JavaScript.

How to remove text without removing inner elements from a parent element using JavaScript?

To remove text without removing inner elements from a parent element using JavaScript, we can loop through all the child nodes in the element and remove the ones with nodeType 3.

For instance, we write:

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

to add a div.

Then we write:

const el = document.getElementById("foo")
let child = el.firstChild
let nextChild;

while (child) {
  nextChild = child.nextSibling;
  if (child.nodeType === 3) {
    el.removeChild(child);
  }
  child = nextChild;
}

to select it with getElementById.

Then we loop through the child elements by first getting the first child with firstChild.

And then we use a while loop to get the nextSibling node.

If the nodeType is 3, then we call removeChild to remove it.

As a result, we shouldn’t see ‘hello world’ in the div after the loop is run.

Conclusion

To remove text without removing inner elements from a parent element using JavaScript, we can loop through all the child nodes in the element and remove the ones with nodeType 3.

Categories
JavaScript Answers

How to calculate the hex color in the middle of a gradient with JavaScript?

Sometimes, we want to calculate the hex color in the middle of a gradient with JavaScript.

In this article, we’ll look at how to calculate the hex color in the middle of a gradient with JavaScript.

How to calculate the hex color in the middle of a gradient with JavaScript?

To calculate the hex color in the middle of a gradient with JavaScript, we can convert the color numbers to decimal, find the halves, and then convert the result back to hex and concatenate them.

For instance, we write:

const color1 = 'FF0000';
const color2 = '00FF00';
const ratio = 0.5;
const hex = (x) => {
  return x.toString(16).padStart(2, '0')
};

const r = Math.ceil(parseInt(color1.substring(0, 2), 16) * ratio + parseInt(color2.substring(0, 2), 16) * (1 - ratio));
const g = Math.ceil(parseInt(color1.substring(2, 4), 16) * ratio + parseInt(color2.substring(2, 4), 16) * (1 - ratio));
const b = Math.ceil(parseInt(color1.substring(4, 6), 16) * ratio + parseInt(color2.substring(4, 6), 16) * (1 - ratio));
const middle = hex(r) + hex(g) + hex(b);
console.log(middle)

We define the hex function that converts the decimal number x to a hex string with toString and the prepend 0’s to it until it reaches the length of 2 with padStart.

Next, we call Math.ceil with the hex color multiplied by the ratio.

Then we add the 2nd color string converted to a decimal number multiplied by 1 - ratio.

Finally, we convert the decimal numbers back to hex strings with hex and concatenate them together.

Therefore, middle is '808000'.

Conclusion

To calculate the hex color in the middle of a gradient with JavaScript, we can convert the color numbers to decimal, find the halves, and then convert the result back to hex and concatenate them.

Categories
JavaScript Answers

How to check if the time is between 7pm and 7am and do something with JavaScript?

Sometimes, we want to check if the time is between 7pm and 7am and do something with JavaScript.

In this article, we’ll look at how to check if the time is between 7pm and 7am and do something with JavaScript.

How to check if the time is between 7pm and 7am and do something with JavaScript?

To check if the time is between 7pm and 7am and do something with JavaScript, we can use the date’s getHours method to get the time.

For instance, we write:

const today = new Date().getHours();
if (today >= 7 && today <= 19) {
  document.body.style.background = "green";
} else {
  document.body.style.background = "blue";
}

to get the hour of the day with getHours.

Then we get if the time is between 7am and 7pm with today >= 7 && today <= 19.

And we set the background to green if it’s true.

Otherwise, we set the background to blue.

Conclusion

To check if the time is between 7pm and 7am and do something with JavaScript, we can use the date’s getHours method to get the time.

Categories
JavaScript Answers

How to not allow typing alphabetic characters in a number input with JavaScript?

Sometimes, we want to not allow typing alphabetic characters in a number input with JavaScript.

In this article, we’ll look at how to not allow typing alphabetic characters in a number input with JavaScript.

How to not allow typing alphabetic characters in a number input with JavaScript?

To not allow typing alphabetic characters in a number input with JavaScript, we can listen for the keypress event and check which key is pressed when the event is triggered.

For instance, we write:

<input>

to add an input.

Then we write:

const input = document.querySelector('input')
input.onkeypress = (evt) => {
  const charCode = evt.keyCode;
  if (charCode != 46 && charCode > 31 &&
    (charCode < 48 || charCode > 57)) {
    evt.preventDefault()
  }
}

to select an input with querySelector.

Then we add a keypress event handler on the input by setting the input.onkeypress property to a function that checks which key is pressed.

If a number key or a period isn’t pressed, then we call evt.preventDefault to stop the character for the key from being appended to the input value.

Conclusion

To not allow typing alphabetic characters in a number input with JavaScript, we can listen for the keypress event and check which key is pressed when the event is triggered.

Categories
JavaScript Answers

How to find the index of the longest array in an array of arrays with JavaScript?

Sometimes, we want to find the index of the longest array in an array of arrays with JavaScript.

In this article, we’ll look at how to find the index of the longest array in an array of arrays with JavaScript.

How to find the index of the longest array in an array of arrays with JavaScript?

To find the index of the longest array in an array of arrays with JavaScript, we can use some array methods and Math.max.

For instance, we write:

const arr = [
  [1, 2, 3, 4, 5],
  [1, 2],
  [1, 1, 1, 1, 2, 2, 2, 2, 4, 4],
  [1, 2, 3, 4, 5]
];
const lengths = arr
  .map(a => a.length)
const index = lengths
  .indexOf(Math.max(...lengths));
console.log(index)

to get the lengths of each array in arr with:

const lengths = arr
  .map(a => a.length)

Then we call lengths.indexOf to return the index of the array with the longest length.

We get the longest length with Math.max(...lengths).

We spread the lengths entries as arguments for Math.max to find the longest length.

Therefore, index is 2 since the 3rd array is the longest.

Conclusion

To find the index of the longest array in an array of arrays with JavaScript, we can use some array methods and Math.max.