Categories
JavaScript Answers

How to count spaces before first character of a string with JavaScript?

Sometimes, we want to count spaces before first character of a string with JavaScript.

In this article, we’ll look at how to count spaces before first character of a string with JavaScript.

How to count spaces before first character of a string with JavaScript?

To count spaces before first character of a string with JavaScript, we can use the JavaScript string’s search method.

For instance, we write:

const index = '    foo'.search(/\S/);
console.log(index)

We call search with a regex to match all non-whitespace characters.

Therefore, index is the index of first character that’s not a space.

index is 4, so any character before index 4 is a space, and so there’re 4 spaces before 'foo'.

Conclusion

To count spaces before first character of a string with JavaScript, we can use the JavaScript string’s search method.

Categories
JavaScript Answers

How to get text for element without children text with JavaScript?

Sometimes, we want to get text for element without children text with JavaScript.

In this article, we’ll look at how to get text for element without children text with JavaScript.

How to get text for element without children text with JavaScript?

To get text for element without children text with JavaScript, we can use the jQuery, contents method.

For instance, we write:

<div id='mydiv'>
  some text here
  <div>
    text for inner div
  </div>
</div>

to add some divs with text.

Then we write:

const texts = $('#mydiv').contents().filter((i, el) => el.nodeType === 3).text()
console.log(texts)

We select the parent div with $('#mydiv').

Then we get the contents of the div with contents.

And then we call filter to return the items with nodeType 3.

Finally, we get the text of the node with text.

As a result, texts is ' some text here'.

Conclusion

To get text for element without children text with JavaScript, we can use the jQuery, contents method.

Categories
JavaScript Answers

How to get max width of child divs with JavaScript?

Sometimes, we want to get max width of child divs with JavaScript.

In this article, we’ll look at how to get max width of child divs with JavaScript.

How to get max width of child divs with JavaScript?

To get max width of child divs with JavaScript, we can select all the divs, spread them into an array, and then get their widths using the clientWidth property with the array map method.

Then we use Math.max to get the max width.

For instance, we write:

<div id="wrapper">
  <div class="image"><img src="https://picsum.photos/180/300
"></div>
  <div class="image"><img src="https://picsum.photos/220/300
"></div>
  <div class="image"><img src="https://picsum.photos/230/300
"></div>
  <div class="image"><img src="https://picsum.photos/100/100
"></div>
  <div class="image"><img src="https://picsum.photos/150/300
"></div>
  <div class="image"><img src="https://picsum.photos/250/300
"></div>
</div>

to add divs with images.

Then we write:

window.onload = () => {
  const widths = [...document.querySelectorAll('.image')].map(i => i.clientWidth)
  const maxWidth = Math.max(...widths)
  console.log(maxWidth)
}

to get the divs with class image with querySelectorAll.

Then we spread them into an array and use map with a callback to return their clientWidth, which is the width of each div.

Next, we call Math.max with the widths array spread into it as arguments.

Therefore, we see that maxWidth is 253.

Conclusion

To get max width of child divs with JavaScript, we can select all the divs, spread them into an array, and then get their widths using the clientWidth property with the array map method.

Then we use Math.max to get the max width.

Categories
JavaScript Answers

How to change a property on an object without mutating it with JavaScript?

Sometimes, we want to change a property on an object without mutating it with JavaScript.

In this article, we’ll look at how to change a property on an object without mutating it with JavaScript.

How to change a property on an object without mutating it with JavaScript?

To change a property on an object without mutating it with JavaScript, we can use the object spread operator.

For instance, we write:

const additions = {
  x: "Bye"
}
const a = {
  x: "Hi",
  y: "Test"
}
const b = {
  ...a,
  ...additions
}
console.log(b)

We create the b object by merging the entries of a with the entries of additions.

The entries that are merged in later overrides the ones that are merged in earlier if they have the same key.

So b.x is 'Bye'.

Therefore, b is {x: 'Bye', y: 'Test'}.

Conclusion

To change a property on an object without mutating it with JavaScript, we can use the object spread operator.

Categories
JavaScript Answers

How to add HTML button click event handlers with JavaScript?

Sometimes, we want to add HTML button click event handlers with JavaScript.

In this article, we’ll look at how to add HTML button click event handlers with JavaScript.

How to add HTML button click event handlers with JavaScript?

To add HTML button click event handlers with JavaScript, we can call addEventListener on the button.

For instance, we write:

<input type="button" id="myButton" value="Add"/>

to add a button.

Then we write:

const myBtn = document.getElementById('myButton');
myBtn.addEventListener('click', (event) => {
  console.log('clicked')
});

We select the button with document.getElementById.

Then we call addEventListener with 'click' and the click event handler respectively.

Therefore, when we click on the button, we see 'clicked' logged.

Conclusion

To add HTML button click event handlers with JavaScript, we can call addEventListener on the button.