dc03kks
dc03kks

Reputation: 221

Send with jquery only the text of a select drop down and not the option value

I have an easy question but I'm stuck and can't get through it.

I have a dropdown select list created from reading a json using jquery, so far I'm ok. My problem is when passing a value to backend (java) to perform search.

I want to pass the text and not the option value, how can I do so?

For example my select is like that:

<select id="ddl_requestTypes" name="ddl_requestTypes" style="width: 250px;">
  <option value="-1"></option>
  <option value="2786">texta</option>
  <option value="2772">textb</option>
  <option value="2773">textc</option>
  <option value="2771">textd</option>
  <option value="2780">texte</option>
</select>

and my function that passes the option value:

$('#LoadRecordsButton').click(function (e) { 
    e.preventDefault();
    if($('#ddl_requestTypes').val() > 0 || $('#ddl_requestTypes').val() != -1) 
    { //here i want to change as well,, is checking the option value were i want to catch the text ONLY 
        $('#MainTableContainer').jtable('load', {
        requestType: $('#ddl_requestTypes').val()  //here is the problem i want only the text!!
        });
    }
    else 
    {
        $('#MainTableContainer').jtable('load');
    }
}); 

but I want to catch only the text

Upvotes: 0

Views: 1084

Answers (2)

user1846747
user1846747

Reputation:

Try this...

$("#ddl_requestTypes option:selected").html();

$(document).ready(function(){
    $('.selected').html("Selected option text is : " + $("#ddl_requestTypes option:selected").html());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<select id="ddl_requestTypes" name="ddl_requestTypes" style="width: 250px;">
<option value="-1"></option>
<option value="2786" selected>texta</option>
<option value="2772">textb</option>
<option value="2773">textc</option>
<option value="2771">textd</option>
<option value="2780">texte</option>
</select>

<div class="selected"></div>

Working JSFiddle http://jsfiddle.net/DivakarDass/rxtoer1d/

Upvotes: 0

tech-gayan
tech-gayan

Reputation: 1413

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

Upvotes: 1

Related Questions