Categories
JavaScript Answers

How to get position of the element of the array with JavaScript?

Sometimes, we want to get position of the element of the array with JavaScript.

In this article, we’ll look at how to get position of the element of the array with JavaScript.

How to get position of the element of the array with JavaScript?

To get position of the element of the array with JavaScript, we can use the array’s indexOf method.

For instance, we write:

const pos = [1, 2, 3, 4].indexOf(3);
console.log(pos)

to call indexOf on [1, 2, 3, 4] with 3 to get the index of 3 in the array.

Therefore, pos is 2 since 3 is in the 3rd position in the array.

Conclusion

To get position of the element of the array with JavaScript, we can use the array’s indexOf method.

Categories
JavaScript Answers

How to add a boolean attribute to an element using JavaScript?

Sometimes, we want to add a boolean attribute to an element using JavaScript.

In this article, we’ll look at how to add a boolean attribute to an element using JavaScript.

How to add a boolean attribute to an element using JavaScript?

To add a boolean attribute to an element using JavaScript, we can call setAttribute to set the boolean attribute to the same value as the attribute name to add the attribute.

We can use removeAttribute to remove the attribute.

For instance, we write:

<input>

to add an input.

Then we write:

const input = document.querySelector('input')
input.setAttribute('disabled', 'disabled');

setTimeout(() => {
  input.removeAttribute('disabled');
}, 3000)

to select the input with querySelector.

Then we call input.setAttribute with 'disabled' and 'disabled' to set the disabled attribute to disabled.

And then we call input.removeAttribute with 'disabled' in the setTimeout callback to remove the attribute in 3 seconds.

Conclusion

To add a boolean attribute to an element using JavaScript, we can call setAttribute to set the boolean attribute to the same value as the attribute name to add the attribute.

We can use removeAttribute to remove the attribute.

Categories
JavaScript Answers

How to check if a string has any non-ASCII characters in it with JavaScript?

Sometimes, we want to check if a string has any non-ASCII characters in it with JavaScript.

In this article, we’ll look at how to check if a string has any non-ASCII characters in it with JavaScript.

How to check if a string has any non-ASCII characters in it with JavaScript?

To check if a string has any non-ASCII characters in it with JavaScript, we can check with a regex.

For instance, we write:

const str = 'abc'
const hasMoreThanAscii = !/^[\u0000-\u007f]*$/.test(str)
console.log(hasMoreThanAscii)

const str2 = '😀'
const hasMoreThanAscii2 = !/^[\u0000-\u007f]*$/.test(str2)
console.log(hasMoreThanAscii2)

to use the /^[\u0000-\u007f]*$/ regex to check if any characters in str and `str2 have only ASCII characters.

ASCII characters have codes ranging from u+0000 to u+007f.

We call test to check if str and str2 matches the regex pattern.

Then we negate the result to check if the string has any non-ASCII characters.

Therefore, hasMoreThanAscii is false and hasMoreThanAscii2 is true.

Conclusion

To check if a string has any non-ASCII characters in it with JavaScript, we can check with a regex.

Categories
JavaScript Answers

How to sort of HTML elements with JavaScript?

Sometimes, we want to sort of HTML elements with JavaScript.

In this article, we’ll look at how to sort of HTML elements with JavaScript.

How to sort of HTML elements with JavaScript?

To sort of HTML elements with JavaScript, we can select them. Then we can spread the selected elements into an array and use the array sort method to do the sorting.

For instance, we write:

<ol>
  <li>foo</li>
  <li>bar</li>
  <li>baz</li>
</ol>

to add 3 li elements.

Then we write:

const lis = document.querySelectorAll('li')
const sorted = [...lis]
  .sort((a, b) => {
    return a.innerHTML.localeCompare(b.innerHTML)
  })
console.log(sorted)

to select the li’s with querySelectorAll.

Then we spread the li elements into an array.

Next, we call sort with a callback that compares the innerHTML of each with localeCompare to sort them alphabetically.

Therefore, sorted is an array of li elements with the one with bar first, the one with baz 2nd, and the one with foo last.

Conclusion

To sort of HTML elements with JavaScript, we can select them. Then we can spread the selected elements into an array and use the array sort method to do the sorting.

Categories
JavaScript Answers

How to decrypt messages with CryptoJS AES and JavaScript?

Sometimes, we want to decrypt messages with CryptoJS AES and JavaScript.

In this article, we’ll look at how to decrypt messages with CryptoJS AES and JavaScript.

How to decrypt messages with CryptoJS AES and JavaScript?

To decrypt messages with CryptoJS AES and JavaScript, we can call the CryptoJS.AES.decrypt method with the encrypted message and the secret key.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>

to add the CryptoJS script.

Then we write:

const key = "secret";
const data = CryptoJS.AES.encrypt("Message", key);
const decrypted = CryptoJS.AES.decrypt(data, key).toString(CryptoJS.enc.Utf8);
console.log(decrypted)

We call CryptoJS.AES.encrypt to encrypt 'Message' with the secret key.

Then we call CryptoJS.AES.decrypt with the encrypted data and the secret key to decrypt data.

Then we convert the decrypted data to a string with toString.

Therefore, decrypted is also 'Message'.

Conclusion

To decrypt messages with CryptoJS AES and JavaScript, we can call the CryptoJS.AES.decrypt method with the encrypted message and the secret key.