0

I have a jqGrid with a drop-down (select) column. When the select's selected option changes, I need to run some validation. I've got the change event firing fine, but I can't figure out the syntax I need to use to get the selected option. Usually, this is simple with this:

$("#someDropDownId option:selected").text();

I can construct the drop-down's ID at runtime, but for the life of me can't figure out how to get the selected text.

var rowId = $("#grid").jqGrid('getGridParam', 'selrow');
var selectId = rowId + '_Description';
//selectId is the ID of the select element, how do I get the selected value now??

I've tried all manner of combinations, for example, $("selectId option:selected").text();, but I can't figure it out. Is it possible, and if so, what is the syntax?

Community
  • 1
  • 1
AJ.
  • 16,368
  • 20
  • 95
  • 150

3 Answers3

2

You could possibly just use the on() method:

$('select').on('change', function(e){
    var selectedOptionText = $(this).find('option:selected').text();
});

JS Fiddle proof of concept.

Or, more simply:

$('select').on('change', function(e){
    var selectedOptionText = $(this).find('option').eq(this.selectedIndex).text();
});

JS Fiddle proof of concept.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
1

If you are inside the event handler you can do $(this).val();

or $(this).find("option:selected").text(); for the text.

Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
Shikyo
  • 1,430
  • 1
  • 14
  • 25
0

Instead of $("selectId option:selected").text() do $("#selectId").val()

Mohsen
  • 64,437
  • 34
  • 159
  • 186