Categories
JavaScript Answers

How to get all selected option text form multi-select using JavaScript?

Sometimes, we want to get all selected option text form multi-select using JavaScript.

In this article, we’ll look at how to get all selected option text form multi-select using JavaScript.

How to get all selected option text form multi-select using JavaScript?

To get all selected option text form multi-select using JavaScript, we can select all the selected options with the checked pseudoselector.

And then we can get the value from them.

For instance, we write:

<select multiple>
  <option>apple</option>
  <option>orange</option>
  <option>grape</option>
  <option>pear</option>
  <option>banana</option>
</select>

to add a multiselect.

Then we write:

const select = document.querySelector('select')
select.onchange = (e) => {
  const selected = [...document.querySelectorAll('select option:checked')]
    .map(item => item.value)
    .join()
  console.log(selected)
}

We get the select element with the document.querySelector.

Then we set the onchange property to a function that gets all the selected options and map them to an array with [...document.querySelectorAll('select option:checked')].

Then we map the value of the array to an array of the selected option element values.

Finally, we call join to join them together.

Conclusion

To get all selected option text form multi-select using JavaScript, we can select all the selected options with the checked pseudoselector.

And then we can get the value from them.

Categories
JavaScript Answers

How to return reference to array item when searching an array of objects with JavaScript?

Sometimes, we want to return reference to array item when searching an array of objects with JavaScript.

In this article, we’ll look at how to return reference to array item when searching an array of objects with JavaScript.

How to return reference to array item when searching an array of objects with JavaScript?

To return reference to array item when searching an array of objects with JavaScript, we can use the JavaScript array find method.

For instance, we write:

const users = [{
  id: 1,
  name: 'name1'
}, {
  id: 2,
  name: 'name2'
}]
const user = users.find(u => {
  const {
    id,
    name
  } = u
  return id === 1 && name === 'name1'
})

We have the users array with some objects.

And we want to get the 1 with id w and name 'name1'.

To do this, we call find with a callback that returns id === 1 && name === 'name1'.

And then user will be the reference to the first entry of users.

Conclusion

To return reference to array item when searching an array of objects with JavaScript, we can use the JavaScript array find method.

Categories
JavaScript Answers

How to detect if we’re in the home page with JavaScript?

Sometimes, we want to detect if we’re in the home page with JavaScript.

In this article, we’ll look at how to detect if we’re in the home page with JavaScript.

How to detect if we’re in the home page with JavaScript?

To detect if we’re in the home page with JavaScript, we can check if window.location.origin is the same as window.location.href.

For instance, we write:

console.log(window.location.origin === window.location.href.slice(0, -1))

to check if window.location.origin is the same as window.location.href without the trailing slash.

Conclusion

To detect if we’re in the home page with JavaScript, we can check if window.location.origin is the same as window.location.href.

Categories
JavaScript Answers

How to expand a text area when clicked on with JavaScript?

Sometimes, we want to expand a text area when clicked on with JavaScript.

In this article, we’ll look at how to expand a text area when clicked on with JavaScript.

How to expand a text area when clicked on with JavaScript?

To expand a text area when clicked on with JavaScript, we can call the animate method.

For instance, we write:

<textarea rows="1" cols="10"></textarea>

to add a text area.

Then we write:

const textarea = document.querySelector('textarea')
textarea.onfocus = () => {
  textarea.animate({
    height: "4em"
  }, 500);
};

to select the text area with document.querySelector.

Then we set the onfocus property of it to a function that calls textarea.animate to set the height of it to 4em for 500 milliseconds.

Conclusion

To expand a text area when clicked on with JavaScript, we can call the animate method.

Categories
JavaScript Answers

How to force the download of a link with JavaScript?

Sometimes, we want to force the download of a link with JavaScript.

In this article, we’ll look at how to force the download of a link with JavaScript.

How to force the download of a link with JavaScript?

To force the download of a link with JavaScript, we can create a hidden link and click it programmatically.

For instance, we write:

<button>
  download
</button>

to add a button.

Then we write:

const download = document.querySelector("button");
download.onclick = () => {
  const anchor = document.createElement("a");
  anchor.href =
    "https://file-examples-com.github.io/uploads/2018/04/file_example_AVI_480_750kB.avi";
  anchor.target = "_blank";
  anchor.download = "example.pdf";
  anchor.click();
};

to select the button with document.querySelector.

Then we set its onclick property to a function that creates the link with document.createElement.

And we set the href to the file we want to download.

And we set the download property to the file name of the file.

Finally, we call click to click it to start the download.

Conclusion

To force the download of a link with JavaScript, we can create a hidden link and click it programmatically.