Categories
JavaScript Answers

How to get a custom button’s text value with JavaScript?

Sometimes, we want to get a custom button’s text value with JavaScript.

In this article, we’ll look at how to get a custom button’s text value with JavaScript.

How to get a custom button’s text value with JavaScript?

To get a custom button’s text value with JavaScript, we can use the textContent or innerText property.

For instance, we write:

<button>
  hello
</button>

to add a button.

Then we write:

const elem = document.querySelector('button');
const txt = elem.textContent || elem.innerText;
console.log(txt)

to select the button with document.querySelector.

And then we get the text content of the button with textContent or innerText.

Therefore, txt is 'hello'.

Conclusion

To get a custom button’s text value with JavaScript, we can use the textContent or innerText property.

Categories
JavaScript Answers

How to add slide down animation from display:none to display:block with JavaScript?

Sometimes, we want to add slide down animation from display:none to display:block with JavaScript.

In this article, we’ll look at how to add slide down animation from display:none to display:block with JavaScript.

How to add slide down animation from display:none to display:block with JavaScript?

To add slide down animation from display:none to display:block with JavaScript, we can use the jQuery hide and slideDown methods.

For instance, we write:

<p>
  hello
</p>

to add a p element.

Then we write:

$('p').stop().css('display', 'block').hide().slideDown();

to select the p element.

Then we call stop to stop the element.

Next, we call css to set the display CSS property to block.

And then we call hide to hide it.

Finally, we call slideDown to make the p element slide down.

Conclusion

To add slide down animation from display:none to display:block with JavaScript, we can use the jQuery hide and slideDown methods.

Categories
JavaScript Answers

How to get HTML code using JavaScript with a URL?

Sometimes, we want to get HTML code using JavaScript with a URL.

In this article, we’ll look at how to get HTML code using JavaScript with a URL.

How to get HTML code using JavaScript with a URL?

To get HTML code using JavaScript with a URL, we can use the fetch function.

For instance, we write:

(async () => {
  const response = await fetch('https://jsfiddle.net/');
  const template = await response.text();
  console.log(template);
})();

We call fetch with the URL of the HTML we want to get.

Then we call response.text to get the response string from the response object.

This only works with the URLs that are in the same domain that the script is hosted in.

Conclusion

To get HTML code using JavaScript with a URL, we can use the fetch function.

Categories
JavaScript Answers

How to wait for a period of time after a function run with JavaScript?

Sometimes, we want to wait for a period of time after a function run with JavaScript.

In this article, we’ll look at how to wait for a period of time after a function run with JavaScript.

How to wait for a period of time after a function run with JavaScript?

To wait for a period of time after a function run with JavaScript, we can use the setTimeout function.

For instance, we write:

setTimeout(() => {
  console.log('hello')
}, 2000);

to call setTimeout with a callback that logs 'hello' in 2000 milliseconds.

Therefore, we see 'hello' logged in 2000 milliseconds after running this code.

Conclusion

To wait for a period of time after a function run with JavaScript, we can use the setTimeout function.

Categories
JavaScript Answers

How to set selection by range in JavaScript?

Sometimes, we want to set selection by range in JavaScript.

In this article, we’ll look at how to set selection by range in JavaScript.

How to set selection by range in JavaScript?

To set selection by range in JavaScript, we can call the document.createRange and window.getSelection methods.

For instance, we write:

<p>
  hello world
</p>

to add a p element.

Then we write:

const node = document.querySelector("p").firstChild;
const range = document.createRange();
range.setStart(node, 0);
range.setEnd(node, 5); 

const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);

to select the text node with:

const node = document.querySelector("p").firstChild;

Next, we call document.createRange to create a range object.

And then we set the selection with setStart and setEnd.

Next, we call window.getSelection to get the selection.

And we call selection.removeAllRanges to clear existing selections.

Finally, we call selection.addRange with range to make the selection.

Conclusion

To set selection by range in JavaScript, we can call the document.createRange and window.getSelection methods.