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
HTML

How to disable drag and drop on HTML elements?

Sometimes, we want to disable drag and drop on HTML elements.

In this article, we’ll look at how to disable drag and drop on HTML elements.

How to disable drag and drop on HTML elements?

To disable drag and drop on HTML elements, we set the draggable attribute to false.

For instance, we write

<div draggable="false"></div>

to set the draggable attribute to false so that the div can’t be dragged.

Conclusion

To disable drag and drop on HTML elements, we set the draggable attribute to false.

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.