Ad

How Do I Get The Current Option Values Into An Array?

- 1 answer

Suppose I have a list:

    <select>
  <option value="Horse">Horse</option>
  <option value="Bird">Bird</option>
  <option value="Dogs">Dogs</option>
  <option value="Cats">Cats</option>
</select>

I know how to grab the values of each option by looping through. My problem is I have code that changes the default values to something if certain conditions are met but if the conditions are not met I need to revert back to the DEFAULT values. I need to get the default values into an array so I can use them to revert back if the conditions are not met...etc. I need pure javascript no frameworks.

So for example if I change <option value="Horse">Horse</option>to <option value="Train">Train</option>I need to be able to store <option value="Horse">Horse</option> BEFORE it gets changed the same way you can get the default value for an input field to revert back to it if needed.

Ad

Answer

I figured out the solution and that is to get the values on page load and make sure your script is before the closing body tag.

Ad
source: stackoverflow.com
Ad