xSketchy0
xSketchy0

Reputation: 91

How to get the HTML text instead of the value from the dropdownlist using jQuery

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.

Upvotes: 0

Views: 35

Answers (1)

Musa
Musa

Reputation: 97672

.innerHTML inside the each

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

Upvotes: 4

Related Questions