Categories
JavaScript Answers

How to get elements by attribute when querySelectorAll with JavaScript?

Sometimes, we want to get elements by attribute when querySelectorAll with JavaScript.

In this article, we’ll look at how to get elements by attribute when querySelectorAll with JavaScript.

How to get elements by attribute when querySelectorAll with JavaScript?

To get elements by attribute when querySelectorAll with JavaScript, we can use the attribute selector.

For instance, we write

const els = document.querySelector('[role="button"]')

to call querySelector with '[role="button"]' to select all the elements that has the role attribute set to button.

Conclusion

To get elements by attribute when querySelectorAll with JavaScript, we can use the attribute selector.

Categories
JavaScript Answers

How to remove value from object without mutation with JavaScript?

Sometimes, we want to remove value from object without mutation with JavaScript.

In this article, we’ll look at how to remove value from object without mutation with JavaScript.

How to remove value from object without mutation with JavaScript?

To remove value from object without mutation with JavaScript, we can use the rest syntax.

For instance, we write

const omitProp = (obj, prop) => {
  const { [prop]: omit, ...res } = obj;
  return res;
};

to create the omitProp function that destructures the prop property from the obj object.

And then we get the rest of the properties in obj with the res object.

Finally, we return res.

Conclusion

To remove value from object without mutation with JavaScript, we can use the rest syntax.

Categories
JavaScript Answers

How to use querySelector with IDs that are numbers with JavaScript?

Sometimes, we want to use querySelector with IDs that are numbers with JavaScript.

In this article, we’ll look at how to use querySelector with IDs that are numbers with JavaScript.

How to use querySelector with IDs that are numbers with JavaScript?

To use querySelector with IDs that are numbers with JavaScript, we can use the attribute value selector.

For instance, we write

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

to select the element with id attribute set to 1.

We have to do this since numbers aren’t valid IDs in CSS.

Conclusion

To use querySelector with IDs that are numbers with JavaScript, we can use the attribute value selector.

Categories
JavaScript Answers

How to detect long press in JavaScript?

Sometimes, we want to detect long press in JavaScript.

In this article, we’ll look at how to detect long press in JavaScript.

How to detect long press in JavaScript?

To detect long press in JavaScript, we can listen for the contextmenu event.

For instance, we write

document.addEventListener('contextmenu', callback);

to call addEventListener to listen for the contextmenu event.

The callback function is called when when the contextmenu event is triggered by long presses.

Conclusion

To detect long press in JavaScript, we can listen for the contextmenu event.

Categories
JavaScript Answers

How to download a file using window.fetch?

Sometimes, we want to download a file using window.fetch.

In this article, we’ll look at how to download a file using window.fetch.

How to download a file using window.fetch?

To download a file using window.fetch, we can call the response object’s blob method.

For instance, we write

const url = "http://example.com/file.doc";
const authHeader = "... ";

const options = {
  headers: {
    Authorization: authHeader,
  },
};
const res = await fetch(url, options);
const blob = await res.blob();
const file = window.URL.createObjectURL(blob);
window.location.assign(file);

to call fetch with the url with the file.

options has the headers for the request.

Then we call res.blob to get the content of the file as a blob.

Next, we call window.URL.createObjectURL with blob to create the file.

And then we call window.location.assign with file to download the file.

Conclusion

To download a file using window.fetch, we can call the response object’s blob method.