0

I'm trying to get the innerHTML/Text from the select options from a dropdownlist instead of the value.

var Array = []; $('#select option').each(function(){Array.push(this.value)});

The method above works, but it only stores the value attribute of the options. For example, if my option were to be <option value="John">Hello</option>, It would store John instead of Hello. I want it to store whatever's in the innerHTML of the option. How do I go about doing this? .innerHTML() before the .each() doesn't work.

2
  • I would like to encourage you to change your variable name from Array to something else. Array is the name of the javascript class for all primitive arrays(Ref. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…). I feel like naming your variable as such is a quick way to lead into unexpected errors. Commented Feb 28, 2018 at 18:12
  • Thank you for the information :), I just simplified the names to make it readable for stackoverflow. Commented Feb 28, 2018 at 18:13

1 Answer 1

4

.innerHTML inside the each

var Array = []; 
$('#select option').each(function(){
  Array.push(this.innerHTML);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.