Skip to content
Advertisement

Get option value in combobox and textarea receive this value to copy

I’m a JavaScript beginner, I want to get the option’s value but in my current code I’m getting its text content.

Example of what I need:

  • User selects option “Blue” -> textarea receives value “sky is blue”. Then on button click “sky is blue” is copied to clipboard.
  • User selects option “Black” -> textarea receives value “I like this color”. Then on button click “I like this color” is copied to clipboard.

The same for the other color.

JavaScript

Advertisement

Answer

If you’re already using vanilla JS for a large part of your code, you don’t need jQuery for this.

You can add an event listener to the select element and get the value of the selected option on change. You can update the textarea‘s value and then select() its content:

JavaScript

Check out the snippet below for a working example:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement