Categories
JavaScript Answers

How to get all global JavaScript variables?

Sometimes, we want to get all JavaScript global variables.

In this article, we’ll look at how to get all JavaScript global variables.

How to get all JavaScript global variables with JavaScript?

To get all JavaScript global variables, we can loop through the window object’s properties.

For instance, we write:

for (const [key, val] of Object.entries(window)) {
  console.log(key, val)
}

to call Object.entries with window to return an array of key-value pair in window as arrays.

In the loop, we log the key and val we destructured from the array.

Conclusion

To get all JavaScript global variables, we can loop through the window object’s properties.

Categories
JavaScript Answers

How to capture a CTRL-S press with JavaScript?

Sometimes, we want to capture a CTRL-S press with JavaScript.

In this article, we’ll look at how to capture a CTRL-S press with JavaScript.

How to capture a CTRL-S press with JavaScript?

To capture a CTRL-S press with JavaScript, we can listen to the keydown event.

For instance, we write:

document.onkeydown = (e) => {
  if (e.ctrlKey && e.key === 's') {
    e.preventDefault();
    console.log('CTRL + S');
  }
}

to set the document.onkeydown property to a function that checks if ctrl+s is pressed with e.ctrlKey && e.key === 's'.

If it’s true, we call e.preventDefault to stop the save file dialog from opening.

And then we log 'CTRL + S'.

Conclusion

To capture a CTRL-S press with JavaScript, we can listen to the keydown event.

Categories
JavaScript Answers

How to remove double quotes from default string with JavaScript?

Sometimes, we want to remove double quotes from default string with JavaScript.

In this article, we’ll look at how to remove double quotes from default string with JavaScript.

How to remove double quotes from default string with JavaScript?

To remove double quotes from default string with JavaScript, we can use the string replace method.

For instance, we write:

const test = "\"House\"";
const s = test.replace(/\"/g, "");
console.log(s)

to call test.replace with /\"/g and an empty string to replace all double quotes with empty strings.

Therefore, s is 'House'.

Conclusion

To remove double quotes from default string with JavaScript, we can use the string replace method.

Categories
JavaScript Answers

How to get an element’s current onclick method’s contents with JavaScript?

Sometimes, we want to get an element’s current onclick method’s contents with JavaScript.

In this article, we’ll look at how to get an element’s current onclick method’s contents with JavaScript.

How to get an element’s current onclick method’s contents with JavaScript?

To get an element’s current onclick method’s contents with JavaScript, we can use the onclick property.

For instance, we write:

<div>
  hello world
</div>

to add a div.

Then we write:

const div = document.querySelector("div")
div.onclick = () => {
  console.log('hello world')
}
console.log(div.onclick)

to select the div with querySelector.

And then we set div.onclick to a function.

Next, we log the content of div.onclick.

As a result, the console log logs

() => {
  console.log('hello world')
}

Conclusion

To get an element’s current onclick method’s contents with JavaScript, we can use the onclick property.

Categories
JavaScript Answers

How to get the text content of an element with JavaScript?

Sometimes, we want to get the text element content of an element with JavaScript.

In this article, we’ll look at how to get the text element content of an element with JavaScript.

How to get the text element content of an element with JavaScript?

To get the text element content of an element with JavaScript, we can use the childNodes to get the child nodes.

And then we use textContent property to get the text content.

For instance, we write:

<div>
  hello world
</div>

to add a div.

Then we write:

const [node] = document.querySelector("div").childNodes
console.log(node.textContent)

to select the div with querySelector and use the childNodes property to get the child nodes of it.

Then we get the first node with destructuring and get its textContent property to get the text in the node.

Conclusion

To get the text element content of an element with JavaScript, we can use the childNodes to get the child nodes.

And then we use textContent property to get the text content.