Categories
JavaScript Answers

How to change the first letter of variable to upper case with JavaScript?

Sometimes, we want to change the first letter of variable to upper case with JavaScript.

In this article, we’ll look at how to change the first letter of variable to upper case with JavaScript.

How to change the first letter of variable to upper case with JavaScript?

To change the first letter of variable to upper case with JavaScript, we can use the string replace method.

For instance, we write

const str = "hello world";
const newStr = str.toLowerCase().replace(/\b[a-z]/g, (letter) => {
  return letter.toUpperCase();
});

to call str.toLowerCase to convert all the letters in str to lower case.

Then we call replace with a regex that gets the first letter of each word.

\b means word boundary, so we get the first lower case letter of each word.

We use the g flag to get all matches.

Then we replace them all with upper case letter by using a function that returns letter.toUpperCase where letter is the match being replaced.

Conclusion

To change the first letter of variable to upper case with JavaScript, we can use the string replace method.

Categories
JavaScript Answers

How to get text of an input text box during onKeyPress with JavaScript?

Sometimes, we want to get text of an input text box during onKeyPress with JavaScript.

In this article, we’ll look at how to get text of an input text box during onKeyPress with JavaScript.

How to get text of an input text box during onKeyPress with JavaScript?

To get text of an input text box during onKeyPress with JavaScript, we can handle the input event.

For instance, we write

<input type="text" onInput="showCurrentValue(event)" />
<br />
<span id="label"></span>

to add an input and a span

Then we write

function showCurrentValue(event) {
  const value = event.target.value;
  document.getElementById("label").innerText = value;
}

to define the showCurrentValue function that takes the input value from event.target.value.

And then we get the span with getElementById and set the span’s innerText to value to show the input value in the span.

Conclusion

To get text of an input text box during onKeyPress with JavaScript, we can handle the input event.

Categories
JavaScript Answers

How to pass this element to JavaScript onclick function and add a class to that clicked element?

Sometimes, we want to pass this element to JavaScript onclick function and add a class to that clicked element.

In this article, we’ll look at how to pass this element to JavaScript onclick function and add a class to that clicked element.

How to pass this element to JavaScript onclick function and add a class to that clicked element?

To pass this element to JavaScript onclick function and add a class to that clicked element, we can set the onclick attribute to a function that’s called with this.

For instance, we write

<div class="row" style="padding-left: 21px">
  <ul class="nav nav-tabs" style="padding-left: 40px">
    <li class="active filter">
      <a href="#month" onclick="onClick(this)">This Month</a>
    </li>
    <li class="filter"><a href="#year" onclick="onClick(this)">Year</a></li>
    <li class="filter">
      <a href="#last60" onclick="onClick(this)">60 Days</a>
    </li>
    <li class="filter">
      <a href="#last90" onclick="onClick(this)">90 Days</a>
    </li>
  </ul>
</div>

to set the onclick attribute to the a elements to call onClick with this.

Then we define the onClick function by writing

function onClick(element) {
  element.removeClass("active");
  element.addClass("active");
}

in our JavaScript code.

We call element.removeClass to remove the 'active' class and we call addClass to add the 'active' class.

Conclusion

To pass this element to JavaScript onclick function and add a class to that clicked element, we can set the onclick attribute to a function that’s called with this.

Categories
JavaScript Answers

How to fix React inline style – style prop expects a mapping from style properties to values, not a string error with JavaScript?

Sometimes, we want to fix React inline style – style prop expects a mapping from style properties to values, not a string error with JavaScript.

In this article, we’ll look at how to fix React inline style – style prop expects a mapping from style properties to values, not a string error with JavaScript.

How to fix React inline style – style prop expects a mapping from style properties to values, not a string error with JavaScript?

To fix React inline style – style prop expects a mapping from style properties to values, not a string error with JavaScript, we set the style prop to an object with the styles.

For instance, we write

<span className="myClass" style={{ float: "left", paddingRight: "5px" }}>
  foobar
</span>

to set the style prop to an object with some style values.

Conclusion

To fix React inline style – style prop expects a mapping from style properties to values, not a string error with JavaScript, we set the style prop to an object with the styles.

Categories
JavaScript Answers

How to fix async function in mocha before() is always finished before it() spec with JavaScript?

Sometimes, we want to fix async function in mocha before() is always finished before it() spec with JavaScript.

In this article, we’ll look at how to fix async function in mocha before() is always finished before it() spec with JavaScript.

How to fix async function in mocha before() is always finished before it() spec with JavaScript?

To fix async function in mocha before() is always finished before it() spec with JavaScript, we can return a promise in the before callback.

For instance, we write

let a = 0;

before(() => {
  return new Promise((resolve) => {
    setTimeout(() => {
      a = 1;
      resolve();
    }, 200);
  });
});

it("a should be set to 1", () => {
  assert(a === 1);
});

to call before with a callback that returns a promise.

Then the it callback should run after the promise in the before callback is finished.

Conclusion

To fix async function in mocha before() is always finished before it() spec with JavaScript, we can return a promise in the before callback.