Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find name of selected option using jQuery

Tags:

I've made a jquery/ajax function that updates #courses, sending #fos's .val() and .text(), specifically of the one that is selected, like so:

$('#selling #fos').change(function() {     $.post('/ajax/courses',         {             fos_id: $('#selling #fos').val(),             name: $('#selling #fos :selected').text()         },     function(data) {         $('#selling #courses').html(data);     }); }); 

How do I extend this function so that it uses 'this', allowing me to reuse this function multiple times on the same page? I'm caught because you can't use name: $(this + ' :selected').text().

like image 652
bafromca Avatar asked Sep 18 '10 16:09

bafromca


1 Answers

This should work:

$("#"+$(this).attr("id")+" :selected") 

it's not pretty but it does the trick :)

or this will work:

$(this).find(":selected").text() 
like image 147
Stefanvds Avatar answered Oct 12 '22 05:10

Stefanvds