Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery access to selectbox text not value how?

example:

<select>
<option value='1'>hello me<option>
<option value='2'>hello world</option>
</select>

how can I access a TEXT not VALUE from select box

so I can display HELLO ME or HELLO WORLD instead of 1 & 2.

like image 430
David King Avatar asked Oct 06 '09 11:10

David King


1 Answers

Use text together with the :selected form selector:

$('select option:selected').text()

Loop over all options' texts and values:

$('#mySelect option').each(function () {
    console.log(this.text + ' ' + this.value);
});

Grab all option texts into an array:

var textArr = $('#mySelect option').map(function (i,n) {
    return $(n).text();
}).get();
console.log(textArr.join(','));
like image 194
karim79 Avatar answered Oct 03 '22 07:10

karim79