I’m trying to grab the text from options in a dropdown list. Assume that the following dropdown has hundreds of options.
<select name="colors" id="colors">
<option value="744">Green</option>
<option value="933">Red</option>
<option value="1838">Yellow</option>
<option value="1839">Blue</option>
</select>
I want to output the text from all options in an array format that I’ll be able to copy and paste in php. Example:
'Green', 'Red', 'Yellow', 'Blue'
This is what I have
var optionText = $("option").text().split().join(", ");
$('.gettext').append(optionText);
I’m not sure how to split by </option> tags and add single quotes around each option item.
Live demo: http://jsfiddle.net/gion_13/tp74c/1/
Iterate through each option and push its text into an array.