Categories
JavaScript Answers

How to change background color of text box input when empty with JavaScript?

Sometimes, we want to change background color of text box input when empty with JavaScript.

In this article, we’ll look at how to change background color of text box input when empty with JavaScript.

How to change background color of text box input when empty with JavaScript?

To change background color of text box input when empty with JavaScript, we can check the input value and set the background.

For instance, we write:

<input type="text">

to add the input.

The we write:

const checkFilled = (input) => {
  if (!input.value) {
    input.style.backgroundColor = 'red'
  } else {
    input.style.backgroundColor = null
  }
}

const input = document.querySelector('input')
input.onchange = (e) => {
  checkFilled(e.target)
}
checkFilled(input)

to create the checkFilled function that takes the input element.

In it, we check if the input value if empty with !input.value.

If it is, we set the background color to red with input.style.backgroundColor = 'red'.

Otherwise, we set the backgroundColor to null to remove the background color.

Next, we select the input with querySelector.

Then we set input.onchange to a function that calls checkFilled with the input, which is the value of e.target.

And we also call checkFilled at the end to run it when the page loads.

Conclusion

To change background color of text box input when empty with JavaScript, we can check the input value and set the background.

Categories
JavaScript Answers

How to add text to SVG document in JavaScript?

Sometimes, we want to add text to SVG document in JavaScript.

In this article, we’ll look at how to add text to SVG document in JavaScript.

How to add text to SVG document in JavaScript?

To add text to SVG document in JavaScript, we can use various DOM manipulation methods.

For instance, we write:

<svg xmlns="https://www.w3.org/2000/svg" xmlns:xlink="https://www.w3.org/1999/xlink" width="300" height="300">
  <g>
  </g>
</svg>

to add an svg element.

Then we write:

const svgNS = "http://www.w3.org/2000/svg";
const newText = document.createElementNS(svgNS, "text");
newText.setAttributeNS(null, "x", 10);
newText.setAttributeNS(null, "y", 30);
newText.setAttributeNS(null, "font-size", "20px");

const textNode = document.createTextNode('hello world');
newText.appendChild(textNode);
document.querySelector("g").appendChild(newText);

to call createElementNS to create the textelement with thesvgNS` namespace.

Then we can setAttributeNS to set attributes on the text element.

Next, we call createTextNode to create a text node with 'hello world' as the text content.

Then we call appendChild with textNode to append the text node to the text element.

Finally, we select the g element with querySelector and call appendChild with the newText text node to append it as the child of g.

Conclusion

To add text to SVG document in JavaScript, we can use various DOM manipulation methods.

Categories
JavaScript Answers

How to partition an array in JavaScript?

Sometimes, we want to partition an array in JavaScript.

In this article, we’ll look at how to partition an array in JavaScript.

How to partition an array in JavaScript?

To partition an array in JavaScript, we can use the Lodash chunk function.

For instance, we write:

const arrayAll = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const chunked = _.chunk(arrayAll, 3)
console.log(chunked)

to call chunk with arrayAll and 3 to return an array with entries in arrayAll divided into chunks of 3.

As a result, chunked is:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Conclusion

To partition an array in JavaScript, we can use the Lodash chunk function.

Categories
JavaScript Answers

How to get the URL parameters inside the HTML page with JavaScript?

Sometimes, we want to get the URL parameters inside the HTML page with JavaScript.

In this article, we’ll look at how to get the URL parameters inside the HTML page with JavaScript.

How to get the URL parameters inside the HTML page with JavaScript?

To get the URL parameters inside the HTML page with JavaScript, we can use the URLSearchParams constructor.

For instance, we write:

const params = new URLSearchParams(document.location.search);
const s = params.get("s");
const o = params.get("o");
console.log(s, o)

to create a URLSearchParams instance with the query string of the current page URL, which we get from document.location.search.

Next, we get the value of the s and o query parameters with params.get.

Therefore, if the current page URL has the query string s=1&o=2, then s is 1, and o is 2.

Conclusion

To get the URL parameters inside the HTML page with JavaScript, we can use the URLSearchParams constructor.

Categories
JavaScript Answers

How to decode a base64 string using CryptoJS with JavaScript?

Sometimes, we want to decode a base64 string using CryptoJS with JavaScript.

In this article, we’ll look at how to decode a base64 string using CryptoJS with JavaScript.

How to decode a base64 string using CryptoJS with JavaScript?

To decode a base64 string using CryptoJS with JavaScript, we can use the CryptoJS.enc.Base64.parse method.

For instance, we write:

const base64 = 'SGVsbG8gd29ybGQ=';
const words = CryptoJS.enc.Base64.parse(base64);
const textString = CryptoJS.enc.Utf8.stringify(words);
console.log(textString)

We call CryptoJS.enc.Base64.parse with the base64 string to parse the base64 string.

Then we call CryptoJS.enc.Utf8.stringify to convert the decrypted words into a string.

Therefore, textString is 'Hello world'.

Conclusion

To decode a base64 string using CryptoJS with JavaScript, we can use the CryptoJS.enc.Base64.parse method.