Categories
JavaScript Answers

How to get a substring before a character with JavaScript?

Sometimes, we want to get a substring before a character with JavaScript.

In this article, we’ll look at how to get a substring before a character with JavaScript.

How to get a substring before a character with JavaScript?

To get a substring before a character with JavaScript, we can use the string’s split method.

For instance, we write:

const string = "foo-bar-baz"
const [foo] = string.split('-')
console.log(foo)

to call string.split with '-‘ to split the string by the dashes into a string array.

Then we get the word before the first dash with destructuring and assign it to foo.

Therefore, foo is 'foo'.

Conclusion

To get a substring before a character with JavaScript, we can use the string’s split method.

Categories
JavaScript Answers

How to add smooth auto scroll by with JavaScript?

Sometimes, we want to add smooth auto scroll by with JavaScript.

In this article, we’ll look at how to add smooth auto scroll by with JavaScript.

How to add smooth auto scroll by with JavaScript?

To add smooth auto scroll by with JavaScript, we can call setTimeout recursively to scroll the page.

For instance, we write:

const pageScroll = () => {
  window.scrollBy(0, 1);
  setTimeout(pageScroll, 10);
}
pageScroll()

to define the pageScroll function to scroll the page.

In it, we call window.scrollBy to scroll 1 pixel down.

Then we call setTimeout with pageScroll and 10 to call pageScroll again 10 milliseconds later.

Conclusion

To add smooth auto scroll by with JavaScript, we can call setTimeout recursively to scroll the page.

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.