Categories
JavaScript Answers

How to convert HTML character entities back to regular text using JavaScript?

Sometimes, we want to convert HTML character entities back to regular text using JavaScript

In Hit article, we’ll look at how to convert HTML character entities back to regular text using JavaScript.

How to convert HTML character entities back to regular text using JavaScript?

To convert HTML character entities back to regular text using JavaScript, we can put the HTML character entities into an element.

Then we can get the regular text from the element.

For instance, we write:

const decodeEntities = (s) => {
  const el = document.createElement('p');
  el.innerHTML = s;
  const str = el.textContent
  return str;
}

console.log(decodeEntities('<'))

to define the decodeEntities that takes the s string.

In it, we create a p element with createElement.

Then we set el.innerHTML to s.

And then we return el.textContent which has the regular text converted from s.

As a result, the console log logs '<'.

Conclusion

To convert HTML character entities back to regular text using JavaScript, we can put the HTML character entities into an element.

Then we can get the regular text from the element.

Categories
JavaScript Answers

How to get span value with JavaScript?

Sometimes, we want to get span value with JavaScript.

In this article, we’ll look at how to get span value with JavaScript.

How to get span value with JavaScript?

To get span value with JavaScript, we can use getElementsByTagName.

For instance, we write:

<div id="test">
  <span>1</span>
  <span>2</span>
  <span>3</span>
  <span>4</span>
</div>

to add a div with spans in it.

Then we write:

const div = document.getElementById("test");
const spans = div.getElementsByTagName("span");

for (const s of spans) {
  console.log(s.innerHTML);
}

to select the div with ID test with getElemetById.

Then we call div.getElementsByTagName with 'span' to select the spans.

Next, we loop through the spans with a for-of loop and log the value of the innerHTML property to log the content of the spans.

Therefore,

"1"
"2"
"3"
"4"

is logged.

Conclusion

To get span value with JavaScript, we can use getElementsByTagName.

Categories
JavaScript Answers

How to find the day difference between two dates (excluding weekend days) with JavaScript?

Sometimes, we want to find the day difference between two dates (excluding weekend days) with JavaScript.

In this article, we’ll look at how to find the day difference between two dates (excluding weekend days) with JavaScript.

How to find the day difference between two dates (excluding weekend days) with JavaScript?

To find the day difference between two dates (excluding weekend days) with JavaScript, we can use a while loop.

For instance, we write:

const getBusinessDatesCount = (startDate, endDate) => {
  let count = 0;
  let curDate = +startDate;
  while (curDate <= +endDate) {
    const dayOfWeek = new Date(curDate).getDay();
    const isWeekend = (dayOfWeek === 6) || (dayOfWeek === 0);
    if (!isWeekend) {
      count++;
    }
    curDate = curDate + 24 * 60 * 60 * 1000
  }
  return count;
}

const d1 = new Date(2022, 1, 1)
const d2 = new Date(2022, 1, 26)
const diff = getBusinessDatesCount(d1, d2)
console.log(diff)

to define the getBusinessDatesCount function which takes the startDate and endDate date objects.

In it, we use a while loop that loops as long as curDate is less than or equal to endDate.

We create dayOfWeek by converting the curDate timestamp to a Date instance and call its getDay method.

Then we check if dayOfWeek is 0 or 6, which is Sunday or Saturday.

And if isWeekend is false, we increment count by 1.

And then we increment curDate by 1 day by adding 24 * 60 * 60 * 1000 milliseconds.

Finally, we return the count and call getBusinessDatesCount with d1 and d2.

Therefore, diff is 19.

Conclusion

To find the day difference between two dates (excluding weekend days) with JavaScript, we can use a while loop.

Categories
JavaScript Answers

How to select td of the table with JavaScript?

Sometimes, we want to select td of the table with JavaScript.

In this article, we’ll look at how to select td of the table with JavaScript.

How to select td of the table with JavaScript?

To select td of the table with JavaScript, we can use some DOM methods.

For instance, we write:

<table>
  <thead>
    <tr>
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>A1</td>
      <td>A2</td>
      <td>A3</td>
    </tr>
    <tr>
      <td>B1</td>
      <td>B2</td>
      <td>B3</td>
    </tr>
    <tr>
      <td>C1</td>
      <td>C2</td>
      <td>C3</td>
    </tr>
  </tbody>
</table>

to add a table.

Then we write:

const table = document.querySelector('table')
const tds = table.querySelectorAll('td')
console.log(tds)

to select the table with querySelector.

Then we call table.querySelectorAll with 'td' to select all the td’s.

As a result, tds is a node list with the td elements.

Conclusion

To select td of the table with JavaScript, we can use some DOM methods.

Categories
JavaScript Answers

How to delete the last character of a string with JavaScript?

Sometimes, we want to delete the last character of a string with JavaScript.

In this article, we’ll look at how to delete the last character of a string with JavaScript.

How to delete the last character of a string with JavaScript?

To delete the last character of a string with JavaScript, we can use the string’s replace method.

For instance, we write:

const s = 'foobar'
const newS = s.replace(/(\s+)?.$/, '')
console.log(newS)

to call s.replace with a regex that matches the last character from the string s and replace that with an empty string.

\s matches whitespaces, and . matches any character.

The $ means the end of the string.

As a result, newS is 'fooba'.

Conclusion

To delete the last character of a string with JavaScript, we can use the string’s replace method.