Categories
JavaScript Answers

How to remove a file from the FileList with JavaScript?

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

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

How to remove a file from the FileList with JavaScript?

To remove a file from the FileList with JavaScript, we can create an array from the FileList object and then remove the entries from the array.

For instance, we write

const input = document.getElementById("files");
const fileListArr = Array.from(input.files);
fileListArr.splice(index, 1);
console.log(fileListArr);

to select the file input with getElementByIf.

Then we get the files selected from the file input with input.files.

And we convert it to an array with Array.from.

Then we call splice with the index of the file we want to remove and 1 to remove the file from the fileListArr array.

Conclusion

To remove a file from the FileList with JavaScript, we can create an array from the FileList object and then remove the entries from the array.

Categories
JavaScript Answers

How to test private functions in unit tests with Mocha and Node.js?

Sometimes, we want to test private functions in unit tests with Mocha and Node.js.

In this article, we’ll look at how to test private functions in unit tests with Mocha and Node.js.

How to test private functions in unit tests with Mocha and Node.js?

To test private functions in unit tests with Mocha and Node.js, we can use the rewire module.

To install it, we run

npm i rewire

Then we use it by writing

const rewire = require("rewire");
const foobar = rewire("./foobar");

describe("private_foobar1", () => {
  const privateFoobar1 = foobar.__get__("privateFoobar1");

  it("should do stuff", (done) => {
    const stuff = privateFoobar1(filter);
    should(stuff).be.ok;
  });
});

to import the ./foobar module that we want to test.

And then we use __get__ to get the privateFoobar1 function from ./foobar.

Then we call the returned privateFoobar1 in our test.

Conclusion

To test private functions in unit tests with Mocha and Node.js, we can use the rewire module.

Categories
JavaScript Answers

How to get the pure text without HTML element using JavaScript?

Sometimes, we want to get the pure text without HTML element using JavaScript.

In this article, we’ll look at how to get the pure text without HTML element using JavaScript.

How to get the pure text without HTML element using JavaScript?

To get the pure text without HTML element using JavaScript, we can use the innerText or textContent properties.

For instance, we write

const element = document.getElementById("txt");
const text = element.innerText || element.textContent;

to select the element with getElementById.

And then we use the innerText or textContent properties to get the text content of the element.

Conclusion

To get the pure text without HTML element using JavaScript, we can use the innerText or textContent properties.

Categories
JavaScript Answers

How to get selected value/text from select on change with JavaScript?

Sometimes, we want to get selected value/text from select on change with JavaScript.

In this article, we’ll look at how to get selected value/text from select on change with JavaScript.

How to get selected value/text from select on change with JavaScript?

To get selected value/text from select on change with JavaScript, we can use the select element’s value property.

For instance, we write

<select id="select_id">
  <option value="0">-Select-</option>
  <option value="1">Communication</option>
</select>

to add a select drop down.

Then we write

const d = document.getElementById("select_id");
d.onchange = () => {
  console.log(d.value);
};

to select the select drop down with getElementById.

And then we set d.onchange to a function that runs when the drop down selection changes.

In it, we get the value attribute value of the selected option element with d.value.

Conclusion

To get selected value/text from select on change with JavaScript, we can use the select element’s value property.

Categories
JavaScript Answers

How to add target=”_blank” to JavaScript window.location with JavaScript?

Sometimes, we want to add target="_blank" to JavaScript window.location with JavaScript.

In this article, we’ll look at how to add target="_blank" to JavaScript window.location with JavaScript.

How to add target="_blank" to JavaScript window.location with JavaScript?

To add target="_blank" to JavaScript window.location with JavaScript, we call window.open with '_blank'.

For instance, we write

window.open("http://www.example.com", "_blank");

to call window.open with the URL we want to open and '_blank' to open the URL in a new tab or window.

Conclusion

To add target="_blank" to JavaScript window.location with JavaScript, we call window.open with '_blank'.