Categories
JavaScript Answers

How to dynamically load google fonts after page has loaded?

Sometimes, we want to dynamically load google fonts after page has loaded.

In this article, we’ll look at how to dynamically load google fonts after page has loaded.

How to dynamically load google fonts after page has loaded?

To dynamically load google fonts after page has loaded, we can add the script tag for Google web font.

Then we write:

WebFont.load({
  google: {
    families: ['Droid Sans', 'Droid Serif']
  }
});

We call Webfont.load with an object to specify the fonts to load.

Conclusion

To dynamically load google fonts after page has loaded, we can add the script tag for Google web font.

Categories
JavaScript Answers

How to get data attribute value of all elements with jQuery?

Sometimes, we want to get data attribute value of all elements with jQuery.

In this article, we’ll look at how to get data attribute value of all elements with jQuery.

How to get data attribute value of all elements with jQuery?

To get data attribute value of all elements with jQuery, we can call the each method on the select element list object.

Then we call data on each element to get the data attribute value.

For instance, we write:

<ul>
  <li class="myClass" data-option-id="2"></li>
  <li class="myClass" data-option-id="15"></li>
  <li class="myClass" data-option-id="27"></li>
</ul>

to add li elements in a ul element.

Then we write:

$('.myClass').each(function() {
  console.log($(this).data('option-id'));
});

to select all elements with class myClass.

Then we call each with a callback to get all the data-option-id attribute values.

We get the value with the data method with 'option-id' as the argument.

As a result, we see 2, 15, and 27 are logged.

Conclusion

To get data attribute value of all elements with jQuery, we can call the each method on the select element list object.

Then we call data on each element to get the data attribute value.

Categories
JavaScript Answers

How to set or change the user’s text selection in JavaScript?

Sometimes, we want to set or change the user’s text selection in JavaScript.

In this article, we’ll look at how to set or change the user’s text selection in JavaScript.

How to set or change the user’s text selection in JavaScript?

To set or change the user’s text selection in JavaScript, we can use the browser selection API.

For instance, we write:

<p>
  hello world
</p>

to add a p element.

Then we write:

const selection = window.getSelection()
const p = document.querySelector('p')
selection.setBaseAndExtent(p, 0, p, 1)

to call window.getSelection to get the selection object.

Then we call selection.setBaseAndExtent to make the select with p to make selections in the p element.

The first argument is the node at the start of the selection.

The 2nd argument is the number of child nodes from the start of the anchor node that should be excluded from the selection.

The 3rd argument is the node at the end of the selection.

And then last argument is the number of child nodes from the start of the node in the 3rd argument that should be included in the selection.

Conclusion

To set or change the user’s text selection in JavaScript, we can use the browser selection API.

Categories
JavaScript Answers

How to check the input value length with JavaScript?

Sometimes, we want to check the input value length with JavaScript.

In this article, we’ll look at how to check the input value length with JavaScript.

How to check the input value length with JavaScript?

To check the input value length with JavaScript, we can set the submit handler of the form to a function that checks the input’s length.

For instance, we write:

<form>
  <input name='title' />
  <input type='submit' />
</form>

to add a form with a text and submit input.

Then we write:

const form = document.querySelector('form');
const input = document.querySelector('input[name="title"]');
form.onsubmit = (e) => {
  e.preventDefault()
  console.log(input.value.length)
}

to select the form and the text input with document.querySelector.

Then we set the form.onsubmit property to a function that calls e.preventDefault to stop the server-side form submission behavior.

Then we check the length of the input value with input.value.length.

Conclusion

To check the input value length with JavaScript, we can set the submit handler of the form to a function that checks the input’s length.

Categories
JavaScript Answers

How to restrict input characters with HTML?

Sometimes, we to restrict input characters with HTML.

In this article, we’ll look at how to restrict input characters with HTML.

How to restrict input characters with HTML?

To restrict input characters with HTML, we can set the type attribute of the input.

For instance, we write:

<input type="number" />

to set the input’s type attribute to number.

Then we can’t type anything but digits into the text input.

Conclusion

To restrict input characters with HTML, we can set the type attribute of the input.