Categories
JavaScript Answers

How to Remove a File from a JavaScript FileList?

Sometimes, we want to remove a file from a JavaScript FileList object.

In this article, we’ll look at how to remove a file from a JavaScript FileList object.

Use the Spread Operator

One way to remove a file from a JavaScript FileList is to use the spread operator to convert the FileList to an array.

Then we can call splice to remove the file we want.

For instance, we can write the following HTML:

<input type='file' multiple id='files'>

We add multiple to let select multiple files.

Then we can remove one of the items selected by writing:

const input = document.getElementById('files')
input.addEventListener('change', () => {
  const fileListArr = [...input.files]
  fileListArr.splice(1, 1)
  console.log(fileListArr)
})

We get the file input element with getElementById .

We spread input.files into an array.

Then we call splice on the array with index 1 as the first argument and we specify that we remove 1 item with the 2nd arguemnt.

Therefore, fileListArr has the 1st and 3rd file listed.

Use the Array.from Method

We can replace the spread operator with the Array.from method.

For instance, we can write:

<input type='file' multiple id='files'>

And:

const input = document.getElementById('files')
input.addEventListener('change', () => {
  const fileListArr = Array.from(input.files)
  fileListArr.splice(1, 1)
  console.log(fileListArr)
})

to do the same thing.

The only difference is that the spread operator is replaced with Array.from .

Conclusion

One way to remove a file from a JavaScript FileList is to use the spread operator to convert the FileList to an array.

Also, we can replace the spread operator with the Array.from method.

Categories
JavaScript Answers

How to Split a String Once in JavaScript?

Sometimes, we want to split a string once in our JavaScript code.

In this article, we’ll look at how to split a string once in our JavaScript code.

Split a String Once in JavaScript

To split a string once in our JavaScript code, we can use the JavaScript string’s indexOf method with the JavaScript string’s slice method to find the index of the first instance of the separator and then do the slicing.

For instance, we can write:

const s = "1|Ceci n'est pas une pipe: | Oui";
const i = s.indexOf('|');
const splits = [s.slice(0, i), s.slice(i + 1)];
console.log(splits)

We have the s string that we want to split by the first | .

Then we call indexOf with '|' to get the index of the first instance of the | symbol.

Next, we call slice with 0 and i to get the substring before the first | .

And likewise, we call slice with i + 1 to get the part after the first | .

We then put both substrings into the splits array.

Therefore, splits is:

["1", "Ceci n'est pas une pipe: | Oui"]

Conclusion

To split a string once in our JavaScript code, we can use the JavaScript string’s indexOf method with the JavaScript string’s slice method to find the index of the first instance of the separator and then do the slicing.

Categories
JavaScript Answers

How to Test if a URL String is Absolute or Relative with JavaScript?

Sometimes, we want to test if a URL string is absolute or relative with JavaScript.

In this article, we’ll look at how to test if a URL string is absolute or relative with JavaScript.

Test if a URL String is Absolute or Relative with JavaScript

To test if a URL string is absolute or relative with JavaScript, we can use a regex to do the check.

For instance, we can write:

const r = new RegExp('^(?:[a-z]+:)?//', 'i');
console.log(r.test('http://example.com'));
console.log(r.test('HTTP://EXAMPLE.COM'));
console.log(r.test('ftp://example.com/file.txt'));
console.log(r.test('//cdn.example.com/lib.js'));
console.log(r.test('test'));

We create the regex with the ^(?:[a-z]+:)?//’ pattern to check if the URL starts something with :// after it and more path segments appended after that.

The part before :// is optional for a URL to be considered absolute.

Then we use the test method to check if each URL is absolute or relative.

If it’s absolute, test returns true . Otherwise, it returns false .

Therefore, the first 4 console logs logs true and the last one logs false .

Conclusion

To test if a URL string is absolute or relative with JavaScript, we can use a regex to do the check.

Categories
JavaScript Answers

How to Detect if an HTML5 Video Element is Playing with JavaScript?

Sometimes, we want to detect if an HTML5 video element is playing with JavaScript.

In this article, we’ll look at how to detect if an HTML5 video element is playing with JavaScript.

Detect if an HTML5 Video Element is Playing with JavaScript

To detect if an HTML5 video element is playing with JavaScript, we can listen to various events emitted by the HTML5 video element.

To do this, we can add the video element with the following HTML:

<div id="output"></div>
<video id="myVideo" width="320" height="176" controls autoplay>
  <source src="https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4" type="video/mp4">
</video>

to add a div to show some text when various events are triggered.

Then we add a video element with the video.

Next, we add the following JavaScript code to listen to video player events and set the div content according to the event triggered:

const media = document.getElementById('myVideo');
const output = document.getElementById('output');

media.addEventListener("playing", () => {
  output.innerHTML = "Playing event triggered";
});

media.addEventListener("pause", () => {
  output.innerHTML = "Pause event triggered";
});

media.addEventListener("seeking", () => {
  output.innerHTML = "Seeking event triggered";
});

media.addEventListener("volumechange", () => {
  output.innerHTML = "Volumechange event triggered";
});

We get the video and div elements with document.getElementById .

Then we call addEventListener on the video player to listen to various events emitted by it.

The 'playing' event is emitted when the video is playing.

The 'pause' event is emitted when the video is paused.

The 'seeking' event is emitted when we’re moving the seek bar.

The 'volumechange' event is emitted when the video volume is changed.

In the event handlers, we set the innerHTML of the div to the content we want to display.

Conclusion

To detect if an HTML5 video element is playing with JavaScript, we can listen to various events emitted by the HTML5 video element.

Categories
JavaScript Answers

How to Get an Element by ID and Set the Value with JavaScript?

To get an element by ID and set the value with JavaScript, we can call document.getElementById with the ID string to get the element by its ID.

Then we can set the innerText property of the retrieved element if it’s not an input element.

Otherwise, we can set the value property of it.

For instance, if we have the following HTML:

<div id="theValue1"></div>  
<input id="theValue2">

Then we can set the innerText of the div by writing:

document.getElementById("theValue1").innerText = "value div";

And we can set the value of the input by writing:

document.getElementById("theValue2").value = "value input";

We call document.getElementById with the value of the id attribute of each element as a string.

Then we set the innerText property value of the div to put some text inside it.

And we set the value property of the input to set the text inside the input box.