Categories
JavaScript Answers

How to See the Result of readAsText() with HTML5 JavaScript File API?

Sometimes, we want to see the result of readAsText() with HTML5 JavaScript File API.

In this article, we’ll look at how to see the result of readAsText() with HTML5 JavaScript File API.

See the Result of readAsText() with HTML5 JavaScript File API

To see the result of readAsText() with HTML5 JavaScript File API, we can get the data from the onload callback.

For instance, we write:

<input type='file'>

to add a file input.

Then we write:

const fr = new FileReader();
fr.onload = (e) => {
  console.log(e.target.result)
};

const input = document.querySelector('input')
input.addEventListener('change', (e) => {
  const [file] = e.target.files
  fr.readAsText(file);
})

to create a FileReader instance and assign it to fr.

Then we set the fr.onload property to a function that gets the text with e.target.result and log it with console.log.

Next, we select the input with document.querySelector.

Then we call input.addEventListener with 'change' to add a change event listener.

The change event is emitted when we select a file.

In the event handler callback, we get the selected files with e.target.files.

Then we call fr.readAsText with file to read the file‘s content as text.

Therefore, when we select a text file with the file input, we see the text display in the console.

Conclusion

To see the result of readAsText() with HTML5 JavaScript File API, we can get the data from the onload callback.

Categories
JavaScript Answers

How to Check if a Path is Absolute or Relative with Node.js?

Sometimes, we want to check if a path is absolute or relative with Node.js.

In this article, we’ll look at how to check if a path is absolute or relative with Node.js.

Check if a Path is Absolute or Relative with Node.js

To check if a path is absolute or relative with Node.js, we can use the isAbsolute method from the path module.

For instance, we can write:

const path = require('path');
const myPath = '/foo/bar'
if (path.isAbsolute(myPath)) {
  console.log('absolute path')
}

to check if myPath is an absolute path with path.isAbsolute.

It should return true since it’s an absolute path.

And therefore, the console log should run.

Conclusion

To check if a path is absolute or relative with Node.js, we can use the isAbsolute method from the path module.

Categories
JavaScript Answers

How to Add a Close Button in div to Close the div with JavaScript?

Sometimes, we want to add a close button in div to close the div with JavaScript.

In this article, we’ll look at how to add a close button in div to close the div with JavaScript.

Add a Close Button in div to Close the div with JavaScript

To add a close button in div to close the div with JavaScript, we can add a click listener to the close button.

For instance, we can write:

<div>
  <button id='close'>
    close
  </button>
  <h3>title</h3>
  <p class="text">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  </p>
</div>

to add a close button in a div.

Then we write:

window.onload = () => {
  document.getElementById('close').onclick = function() {
    this.parentNode.remove()
    return false;
  };
};

We set the window.onload property to a function that runs when the DOM is loaded.

In the function, we select the close button with document.getElementById('close').

And we set the onclick property to a function that removes the the parent of the close button, which is the div.

We get the parent of the close button with this.parentNode.

Then we call remove on it to remove the parent node which is the div.

And we return false to prevent the default action.

Now when we click the close button, the div is removed from the screen.

Conclusion

To add a close button in div to close the div with JavaScript, we can add a click listener to the close button.

Categories
JavaScript Answers

How to Run JavaScript Code When we Click on a Link?

Sometimes, we want to run JavaScript code when we click on a link.

In this article, we’ll look at how to run JavaScript code when we click on a link.

Run JavaScript Code When we Click on a Link

To run JavaScript code when we click on a link, we can attach a click event listener to the a element.

For instance, we can write:

<a href="#">LinkText</a>

to add an a tag.

We set href to # to avoid going to any URL when we click on the link.

Then we write:

const a = document.querySelector('a')
a.addEventListener('click', () => {
  console.log('clicked')
})

to get the a element with document.querySelector.

Then we call addEventListener with 'click' to add a click listener.

The 2nd argument is a callback that runs when we click on the link.

Therefore, we should see 'clicked' logged when we click on the link.

Conclusion

To run JavaScript code when we click on a link, we can attach a click event listener to the a element.

Categories
JavaScript Answers

How to Create Array from a for Loop with JavaScript?

Sometimes, we want to create an array from a for loop with JavaScript.

In this article, we’ll look at how to create an array from a for loop with JavaScript.

Create Array from a for Loop with JavaScript

To create an array from a for loop with JavaScript, we can use the JavaScript array push method to append the entries into the array within the loop body.

For instance, we can write:

const yearStart = 2000;
const yearEnd = 2040;
const arr = [];

for (let i = yearStart; i < yearEnd + 1; i++) {
  arr.push(i);
}

console.log(arr)

We set the yearStart and yearEnd variables to the start and end values of the for loop.

Then we loop from yearStart to yearEnd with the for loop with:

for (let i = yearStart; i < yearEnd + 1; i++)

Then in the loop body, we call arr.push with i to push i into the arr array.

Therefore, from the console log, we see that arr is:

[2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039, 2040]

Conclusion

To create an array from a for loop with JavaScript, we can use the JavaScript array push method to append the entries into the array within the loop body.