Categories
JavaScript Answers

What is the async await equivalent of Promise.resolve().then() in JavaScript?

Sometimes, we want to find the async and await equivalent of Promise.resolve().then() in JavaScript.

In this article, we’ll look at how the async and await equivalent of Promise.resolve().then() in JavaScript.

What is the async await equivalent of Promise.resolve().then() in JavaScript?

The async and await equivalent of Promise.resolve().then() in JavaScript is the following:

const fn = async () => {}

(async () => {
  await fn()
})()

fn is the same as Promise.resolve which returns a promise that resolves to an empty value.

We call it and use await so that we wait until fn resolves until we run the next line of code.

Conclusion

The async and await equivalent of Promise.resolve().then() in JavaScript is an async function that returns nothing and adding await before the async function call.

Categories
JavaScript Answers

How to detect the value of input field after a keydown event in the input field with JavaScript?

Sometimes, we want to detect the value of input field after a keydown event in the input field with JavaScript.

In this article, we’ll look at how to detect the value of input field after a keydown event in the input field with JavaScript.

How to detect the value of input field after a keydown event in the input field with JavaScript?

To detect the value of input field after a keydown event in the input field with JavaScript, we can use the value property.

For instance, we write:

<input>

to add an input.

Then we write:

const input = document.querySelector('input')
input.onkeydown = (e) => {
  console.log(e.target.value)
}

to select the input with querySelector.

And then we set the input.onkeydown property to a function that logs the input value, which is stored in e.target.value.

Conclusion

To detect the value of input field after a keydown event in the input field with JavaScript, we can use the value property.

Categories
JavaScript Answers

How to get an HTML element in which a mouse click occurred with JavaScript?

Sometimes, we want to get an HTML element in which a mouse click occurred with JavaScript.

In this article, we’ll look at how to get an HTML element in which a mouse click occurred with JavaScript.

How to get an HTML element in which a mouse click occurred with JavaScript?

To get an HTML element in which a mouse click occurred with JavaScript, we can use the e.target property in the document’s click event listener.

For instance, we write:

<div>
  foo
</div>
<section>
  bar
</section>

to add some elements.

Then we write:

document.onclick = (e) => {
  console.log(e.target.tagName)
}

to add a click event listener to document which is the page.

In the click listener, we log the e.target.tagName, which has the tag name of the element that we clicked on.

e.target is the element we clicked on.

Therefore, when we click on the div, we see 'DIV' logged.

And when we click on the section element, we see 'SECTION' logged.

Conclusion

To get an HTML element in which a mouse click occurred with JavaScript, we can use the e.target property in the document’s click event listener.

Categories
JavaScript Answers

How to set or copy JavaScript computed style from one element to another?

Sometimes, we want to set or copy JavaScript computed style from one element to another.

In this article, we’ll look at how to set or copy JavaScript computed style from one element to another.

How to set or copy JavaScript computed style from one element to another?

To set or copy JavaScript computed style from one element to another, we can loop through each style and call the setProperty method to set the styles on the target element.

For instance, we write:

<div style='background-color: green; height: 100px; width: 100px'>

</div>
<section></section>

to add a div and a section element.

Then we copy the styles of the div to the section element by writing:

const copyNodeStyle = (sourceNode, targetNode) => {
  const computedStyle = window.getComputedStyle(sourceNode);
  for (const key of computedStyle) {
    targetNode.style.setProperty(key, computedStyle.getPropertyValue(key), computedStyle.getPropertyPriority(key))
  }
}

const div = document.querySelector('div')
const section = document.querySelector('section')
copyNodeStyle(div, section)

We define the copyNode function that takes the sourceNode and targetNode.

In it, we get the sourceNode‘s styles with window.getComputedStyle.

Then we loop through the computedStyle iterable object with the for-of loop.

We get the style from the sourceNode with computedStyle.getPropertyValue(key).

And we get the priority of the style with computedStyle.getPropertyPriority(key).

Then we set the styles with setProperty.

As a result, we see that the section element has the same styles as the div after the loop is run.

Conclusion

To set or copy JavaScript computed style from one element to another, we can loop through each style and call the setProperty method to set the styles on the target element.

Categories
JavaScript Answers

How to select DOM elements in Chrome console with JavaScript?

Sometimes, we want to select DOM elements in Chrome console with JavaScript.

In this article, we’ll look at how to select DOM elements in Chrome console with JavaScript.

How to select DOM elements in Chrome console with JavaScript?

To select DOM elements in Chrome console with JavaScript, we can use the console.dir method to see the selected element’s properties.

For instance, we type in:

console.dir(document.getElementById("hello"));

to select the element with ID hello with getElementById.

Then we pass that in as the argument of console.dir to see the properties of the selected element object instead of its markup.

Conclusion

To select DOM elements in Chrome console with JavaScript, we can use the console.dir method to see the selected element’s properties.