Categories
JavaScript Answers

How to show just the first line of text of a div and expand on click with JavaScript?

Sometimes, we want to show just the first line of text of a div and expand on click with JavaScript.

In this article, we’ll look at how to show just the first line of text of a div and expand on click with JavaScript.

How to show just the first line of text of a div and expand on click with JavaScript?

To show just the first line of text of a div and expand on click with JavaScript, we can set the height of the div.

For instance, we write:

<div>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas nibh tortor, consectetur in felis nec, elementum condimentum felis. Aliquam et condimentum dui. Quisque luctus purus mauris, non fringilla lectus mollis sit amet. Vivamus vitae lectus ut ex tristique tincidunt eget ac urna.
</div>

to add a div with text.

Then we write:

div {
  border: 1px solid red;
  height: 1em;
  padding: 2px;
  overflow: hidden
}

to set the height of the div to show a single line.

Next, we write:

const div = document.querySelector('div')
div.onclick = (e) => {
  e.target.style.height = 'auto'
}

to select the div with querySelector.

And then we set div.onclick to a function that sets the height of the div to 'auto'.

Conclusion

To show just the first line of text of a div and expand on click with JavaScript, we can set the height of the div.

Categories
JavaScript Answers

How to get last week’s date with JavaScript?

Sometimes, we want to get last week’s date with JavaScript.

In this article, we’ll look at how to get last week’s date with JavaScript.

How to get last week’s date with JavaScript?

To get last week’s date with JavaScript, we can get the timestamp of the original date and subtract 7 * 24 * 60 * 60 * 1000 from it.

For instance, we write:

const firstDay = new Date(2022, 1, 2)
const previousWeek = new Date(firstDay.getTime() - 7 * 24 * 60 * 60 * 1000);
console.log(previousWeek)

to create the firstDay date.

Then we get the timestamp of firstDay in milliseconds with getTime.

And then we subtract 7 days from firstDay in milliseconds and create a new Date instance from the returned timestamp.

Therefore, previousWeek is Wed Jan 26 2022 00:00:00 GMT-0800 (Pacific Standard Time).

Conclusion

To get last week’s date with JavaScript, we can get the timestamp of the original date and subtract 7 * 24 * 60 * 60 * 1000 from it.

Categories
JavaScript Answers

How to remove empty array values from an array with JavaScript?

Sometimes, we want to remove empty array values from an array with JavaScript.

In this article, we’ll look at how to remove empty array values from an array with JavaScript.

How to remove empty array values from an array with JavaScript?

To remove empty array values from an array with JavaScript, we can call array’s filter method with Boolean.

For instance, we write:

const arr = ['One', 'Two', '', 'Four', '', ''];
const newArr = arr.filter(Boolean);
console.log(newArr);

to call arr.filter with Boolean to return an array with the falsy values filtered out.

Empty strings are falsy, so they’ll be filtered out.

Therefore, newArr is ["One", "Two", "Four"].

Conclusion

To remove empty array values from an array with JavaScript, we can call array’s filter method with Boolean.

Categories
JavaScript Answers

How to loop through a string with JavaScript?

Sometimes, we want to loop through a string with JavaScript.

In this article, we’ll look at how to loop through a string with JavaScript.

How to loop through a string with JavaScript?

To loop through a string with JavaScript, we can use the for-of loop.

For instance, we write:

const str = '123456';
for (const s of str) {
  console.log(s)
}

to loop through each character of str with the for-of loop.

s is the character of str being looped through.

Therefore, we see:

"1"
"2"
"3"
"4"
"5"
"6"

logged.

Conclusion

To loop through a string with JavaScript, we can use the for-of loop.

Categories
JavaScript Answers

How to highlight all text occurrences in a HTML page with JavaScript?

Sometimes, we want to highlight all text occurrences in a HTML page with JavaScript.

In this article, we’ll look at how to highlight all text occurrences in a HTML page with JavaScript.

How to highlight all text occurrences in a HTML page with JavaScript?

To highlight all text occurrences in a HTML page with JavaScript, we can use the string replace method.

For instance, we write:

<div>
  foo bar foo baz
</div>

to add a div.

Then we write:

const div = document.querySelector('div')
div.innerHTML = div.innerHTML.replace(/foo/g, (match) => {
  return `<span style="background-color: yellow">${match}</span>`
})

to select the div with querySelector.

And then we add the highlight by using div.innerHTML to get the div’s HTML code.

Then we call replace with a regex to match all instances of 'foo' and a callback that returns 'foo' wrapped with a span with the background color style set to yellow.

Finally, we assign the returned HTML string to div.innerHTML.

As a result, we should see all instances of foo highlighted in yellow on the screen.

Conclusion

To highlight all text occurrences in a HTML page with JavaScript, we can use the string replace method.