Categories
JavaScript Answers

How to get the current directory name in JavaScript?

Sometimes, we want to get the current directory name in JavaScript.

In this article, we’ll look at how to get the current directory name in JavaScript.

How to get the current directory name in JavaScript?

To get the current directory name in JavaScript, we can use the window.location.pathname property.

For instance, we write

const loc = window.location.pathname;
const dir = loc.substring(0, loc.lastIndexOf("/"));

to use `window.location.pathname to get the full path.

Then we get the current directory name by getting the last segment of the path string with

loc.substring(0, loc.lastIndexOf("/"))

Conclusion

To get the current directory name in JavaScript, we can use the window.location.pathname property.

Categories
JavaScript Answers

How to fix Uncaught SyntaxError: Failed to execute ‘querySelector’ on ‘Document’ error with JavaScript?

Sometimes, we want to fix Uncaught SyntaxError: Failed to execute ‘querySelector’ on ‘Document’ error with JavaScript.

In this article, we’ll look at how to fix Uncaught SyntaxError: Failed to execute ‘querySelector’ on ‘Document’ error with JavaScript.

How to fix Uncaught SyntaxError: Failed to execute ‘querySelector’ on ‘Document’ error with JavaScript?

To fix Uncaught SyntaxError: Failed to execute ‘querySelector’ on ‘Document’ error with JavaScript, we should make sure we call it with a valid selector string.

For instance, we write

const el = document.querySelector("[id='22']");

to call querySelector with "[id='22']" to get the element with ID 22 since valid IDs can’t start with numbers.

Conclusion

To fix Uncaught SyntaxError: Failed to execute ‘querySelector’ on ‘Document’ error with JavaScript, we should make sure we call it with a valid selector string.

Categories
JavaScript Answers

How to make a test fail with Jest and JavaScript?

Sometimes, we want to make a test fail with Jest and JavaScript.

In this article, we’ll look at how to make a test fail with Jest and JavaScript.

How to make a test fail with Jest and JavaScript?

To make a test fail with Jest and JavaScript, we can throw an error in our test.

For instance, we write

test("test", () => {
  throw new Error("I have failed you");
});

to throw an Error object in our test callback to make the test fail.

Conclusion

To make a test fail with Jest and JavaScript, we can throw an error in our test.

Categories
JavaScript Answers

How to add Drop-Down list programmatically with JavaScript?

Sometimes, we want to add drop-down list programmatically with JavaScript.

In this article, we’ll look at how to add drop-down list programmatically with JavaScript.

How to add Drop-Down list programmatically with JavaScript?

To add drop-down list programmatically with JavaScript, we can use some DOM manipulation methods.

For instance, we write

const myParent = document.body;
const array = ["Volvo", "Saab", "Mercades", "Audi"];

const selectList = document.createElement("select");
selectList.id = "mySelect";
myParent.appendChild(selectList);

for (const a of array) {
  const option = document.createElement("option");
  option.value = a;
  option.text = a;
  selectList.appendChild(option);
}

to get the body element with document.body.

Then we create a select element with createElement.

Next, we set its id with

selectList.id = "mySelect";

Then we append the select element as the last child of the body with

myParent.appendChild(selectList);

Next, we loop through the array with a for-of loop.

In it, we create an option element with createElement.

Then we set its value attribute and the text of the option element.

And then we call selectList.appendChild with option to append the option as the last child of the select.

Conclusion

To add drop-down list programmatically with JavaScript, we can use some DOM manipulation methods.

Categories
JavaScript Answers

How to convert relative path to absolute using JavaScript?

Sometimes, we want to convert relative path to absolute using JavaScript.

In this article, we’ll look at how to convert relative path to absolute using JavaScript.

How to convert relative path to absolute using JavaScript?

To convert relative path to absolute using JavaScript, we can use the URL constructor.

For instance, we write

const url = new URL("../mypath", "http://www.example.com/search").href;

to create a new URL object with the relative path and the base path.

Then we get the absolute path with href.

As a result, url is 'http://www.example.com/mypath'.

Conclusion

To convert relative path to absolute using JavaScript, we can use the URL constructor.