Categories
JavaScript Answers

How to Retrieve Data from a JavaScript ReadableStream Object?

Sometimes, we want to retrieve data from a JavaScript ReadableStream object.

In this article, we’ll look at how to retrieve data from a JavaScript ReadableStream object.

Retrieve Data from a JavaScript ReadableStream Object

To retrieve data from a JavaScript ReadableStream object, we need to call a conversion method to convert the ReadableStream to the actual data we can use.

For instance, we write:

(async () => {
  const response = await fetch("https://httpbin.org/ip");
  const body = await response.json();
  console.log(body)
})()

to make a GET request to https://httpbin.org/ip with fetch.

fetch returns a promise that resolves to a ReadableStream object which we assigned to response.

Then to convert that to the response data by calling the json method since the response data is serialized into JSON format.

json also returns a promise so we have to use await on that and assign the resolved value to body.

Therefore, body should have the data from the ReadableStream object.

Conclusion

To retrieve data from a JavaScript ReadableStream object, we need to call a conversion method to convert the ReadableStream to the actual data we can use.

Categories
JavaScript Answers

How to Play a Beep Sound on Button Click with JavaScript?

Sometimes, we want to play a beep sound on button click with JavaScript.

In this article, we’ll look at how to play a beep sound on button click with JavaScript.

Play a Beep Sound on Button Click with JavaScript

To play a beep sound on button click with JavaScript, we can add an event listener to a button.

And in the event listener, we can call the play method on the Audio instance with the URL set to the beep sound clip.

For instance, we write:

<button>
  foo
</button>

to add a button.

Then we write:

const audio = new Audio('https://www.soundjay.com/buttons/beep-01a.mp3')
const button = document.querySelector('button')

button.addEventListener('click', (e) => {
  audio.play()
})

to create an Audio instance with the beep sound URL and assign it to audio.

Then we select the button with document.querySelector.

And finally, we call button.addEventListener with 'click' to add a click event listener.

The 2nd argument is a callback that runs when we click the button.

In it, we call audio.play to play the clip.

Therefore, when we click the button, the beep sound clip will play.

Conclusion

To play a beep sound on button click with JavaScript, we can add an event listener to a button.

And in the event listener, we can call the play method on the Audio instance with the URL set to the beep sound clip.

Categories
JavaScript Answers

How to Remove Values from Select List Based on Condition with JavaScript?

Sometimes, we want to remove values from select list based on condition with JavaScript.

In this article, we’ll look at how to remove values from select list based on condition with JavaScript.

Remove Values from Select List Based on Condition with JavaScript

To remove values from select list based on condition with JavaScript, we can loop through the option elements to find the element we want to remove with the for-of loop.

Then once we found the element we want to remove, then we call remove on it to remove it.

For instance, we write:

<select id="mySelect" name="val" size="1" >
    <option value="A">Apple</option>
    <option value="C">Cars</option>
    <option value="H">Honda</option>
    <option value="F">Fiat</option>
    <option value="I">Indigo</option>                    
</select> 

to add the select element with some options.

Then we write:

const selectObject = document.getElementById("mySelect");

for (const o of selectObject.options) {
  if (o.value === 'A') {
    o.remove();
  }
}

to select the select element with document.getElementById.

Then we loop through each option element in the for-of loop.

The option elements for the select are stored in selectObject.options.

We check if o.value is 'A' with the if statement.

And if it is, we call o.remove to remove the o option element.

Therefore, we see a drop down without the choice with text Apple in it.

Conclusion

To remove values from select list based on condition with JavaScript, we can loop through the option elements to find the element we want to remove with the for-of loop.

Then once we found the element we want to remove, then we call remove on it to remove it.

Categories
JavaScript Answers

How to Get the Date of Next Monday with JavaScript?

Sometimes, we want to get the date of next Monday with JavaScript.

In this article, we’ll look at how to get the date of next Monday with JavaScript.

Get the Date of Next Monday with JavaScript

To get the date of next Monday with JavaScript, we can use the setDate, getDate and getDay methods.

To use them, we write:

const d = new Date();
d.setDate(d.getDate() + ((7 - d.getDay()) % 7 + 1) % 7);
console.log(d);

We create a date object with today’s date and time with the Date constructor and assign it to d.

Then we call setDate with the date for next Monday.

To get the date for next Monday, we first get the day of the month for today’s date with getDate.

Then we add the number of days until the date reaches Monday.

To get the difference between next Monday and today, we call getDay to get the day of the week.

Then we multiply that by -1 and add 7 to the returned result, which is:

7 - d.getDay()

Then we get the remainder of that divided by 7, which is:

(7 - d.getDay()) % 7

And then we add 1 to the whole expression.

This will get the day of the month for next Monday or the Monday after, so we have to get the remainder of that divided by 7 again.

And we add that to the date of d.

After we call setDate with the whole expression, we get the date and time for next Monday.

Conclusion

To get the date of next Monday with JavaScript, we can use the setDate, getDate and getDay methods.

Categories
JavaScript Answers

How to Auto-Highlight an Input Field on Focus with JavaScript?

Sometimes, we want to auto-highlight an input field on focus with JavaScript.

In this article, we’ll look at how to auto-highlight an input field on focus with JavaScript.

Auto-Highlight an Input Field on Focus with JavaScript

To auto-highlight an input field on focus with JavaScript, we can listen to the focus event.

And in the event handler, we can call the select method on the input to highlight it.

For instance, if we have the following input:

<input type='text' id='url-uinput' value='http://www.example.com/' />

We write:

const input = document.getElementById("url-uinput")

input.addEventListener('focus', () => {
  input.select();
})

to select the input with the document.getElementById method.

Then we call input.addEventListener with 'focus' to listen for the focus event.

We pass in an event handler function as the 2nd argument.

And in it, we call input.select to select the text when we focus on the input box.

Now when we click on the input box, we see all the text in it highlighted.

Conclusion

To auto-highlight an input field on focus with JavaScript, we can listen to the focus event.

And in the event handler, we can call the select method on the input to highlight it.