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.
  • 15,808
  • 20
  • 92
  • 148

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
  • 240,457
  • 50
  • 366
  • 401
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
  • 28,138
  • 6
  • 71
  • 113
Shikyo
  • 1,175
  • 1
  • 11
  • 20
0

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

Mohsen
  • 61,836
  • 32
  • 154
  • 180