Categories
JavaScript Answers

How to extract values from an HTML date input with JavaScript?

Sometimes, we want to extract values from an HTML date input with JavaScript.

In this article, we’ll look at how to extract values from an HTML date input with JavaScript.

How to extract values from an HTML date input with JavaScript?

To extract values from an HTML date input with JavaScript, we can add a change event listener to the date input.

Then we can get the input value from the e.target.value property in the event listener.

For instance, we write:

<input type="date" />

to add an input.

Then we write:

const input = document.querySelector('input')
input.onchange = (e) => {
  console.log(e.target.value)
}

to select an input.

Then we set the input.onchange property to a function that logs the e.target.value, which is the selected date string.

The selected date will be a ISO-8601 format string.

Conclusion

To extract values from an HTML date input with JavaScript, we can add a change event listener to the date input.

Then we can get the input value from the e.target.value property in the event listener.

Categories
JavaScript Answers

How to add an alias for an object property in JavaScript?

Sometimes, we want to add an alias for an object property in JavaScript.

In this article, we’ll look at how to add an alias for an object property in JavaScript.

How to add an alias for an object property in JavaScript?

To add an alias for an object property in JavaScript, we can add a getter.

For instance, we write:

const obj = {
  a: 1,
  get b() {
    return this.a
  }
}

console.log(obj.a)
console.log(obj.b)

obj.a = 2
console.log(obj.a)
console.log(obj.b)

to add the b getter which returns the latest value of a.

Therefore, from the first 2 console logs, we see that obj.a and obj.b are both 1.

Then after we set obj.a to 2, we see that obj.a and obj.b from the console log.

Conclusion

To add an alias for an object property in JavaScript, we can add a getter.

Categories
JavaScript Answers

How to add different fillStyle colors for arc in canvas with JavaScript?

Sometimes, we want to add different fillStyle colors for arc in canvas with JavaScript.

In this article, we’ll look at how to add different fillStyle colors for arc in canvas with JavaScript.

How to add different fillStyle colors for arc in canvas with JavaScript?

To add different fillStyle colors for arc in canvas with JavaScript, we can draw 2 different arcs.

For instance, we write:

<canvas width='400' height='400'></canvas>

to add a canvas element.

Then we write:

const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d')
ctx.fillStyle = "red";
ctx.beginPath();
ctx.arc(15, 15, 15, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fill();

ctx.fillStyle = "green";
ctx.beginPath();
ctx.arc(100, 15, 15, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fill();

We select the canvas with querySelector.

Then we call getContext to get the context.

Next, we set the fillStyle for the arc.

Then we call arc with the coordinates of the top left corner, radius, start and end angle, and whether we want to draw counterclockwise respectively.

We call closePath and fill to draw and fill the arc respectively.

Likewise, we draw the 2nd arc the same way but with different fillStyle.

Now we see a red and green circle drawn.

Conclusion

To add different fillStyle colors for arc in canvas with JavaScript, we can draw 2 different arcs.

Categories
JavaScript Answers

How to remove last segment from URL with JavaScript?

Sometimes, we want to remove last segment from URL with JavaScript.

In this article, we’ll look at how to remove last segment from URL with JavaScript.

How to remove last segment from URL with JavaScript?

To remove last segment from URL with JavaScript, we can use the string’s slice method.

For instance, we write:

const url = 'http://example.com/foo/bar'
const newUrl = url.slice(0, url.lastIndexOf('/'));
console.log(newUrl)

We call url.slice with the indexes of the start and end of the substring we want to return.

The character at the end index itself is excluded.

We have url.lastIndexOf('/') to return the index of the last / in the URL string.

Therefore, newUrl is 'http://example.com/foo'.

Conclusion

To remove last segment from URL with JavaScript, we can use the string’s slice method.

Categories
JavaScript Answers

How to append a param onto the current URL with JavaScript?

Sometimes, we want to append a param onto the current URL with JavaScript.

In this article, we’ll look at how to append a param onto the current URL with JavaScript.

How to append a param onto the current URL with JavaScript?

To append a param onto the current URL with JavaScript, we can create a new URL instance from the URL string.

Then we can call the searchParams.append method on the URL instance to append a new URL parameter into it.

For instance, we write:

const url = new URL("http://foo.bar/?x=1&y=2");
url.searchParams.append('z', 42);
const newUrl = url.toString();
console.log(newUrl)

to create a new URL instance with "http://foo.bar/?x=1&y=2".

Then we call url.searchParams.append with the URL param key and value respectively.

And then we call toString to return the new URL string.

Therefore, newUrl is 'http://foo.bar/?x=1&y=2&z=42'.

Conclusion

To append a param onto the current URL with JavaScript, we can create a new URL instance from the URL string.

Then we can call the searchParams.append method on the URL instance to append a new URL parameter into it.