V.V
V.V

Reputation: 883

Get the value form dropdown using Jquery

I use the following jquery function for get the drop down value in a table row.But its not give the value.

HTML

<select id='statusprocess'><option value='optSelected'>Selected</option><option value='optNotSelected'>Not Selected</option></select>

JQUERY

     var select = $(this).closest('tr').find('input[type="select"]').map(function () {
                    return this.text();
                }).get();

How to find the dropdown and get selected option text?

Answer Is:

This is the Exact way to find the dropdown in Table and get selected the Selected Text.

var select = $(this).closest('tr').find('select option:selected').text();

It was Working Fine Now.

Upvotes: 1

Views: 11021

Answers (6)

V.V
V.V

Reputation: 883

This is the Exact way to find the dropdown in Table and get selected the Selected Text.

var select = $(this).closest('tr').find('select option:selected').text();

It was Working Fine Now.

Upvotes: 0

Ishan Jain
Ishan Jain

Reputation: 8161

There is no input[type=select] exist.

Your dropdown list is a <select> element, not an <input type="select" />. So you should use the select instead of using input[type = "select"].

Selected option value:

$(this).closest('tr').find('#statusprocess').val();

Selected option text:

$(this).closest('tr').find('#statusprocess option:selected').text();

Working Example

OR if you have single dropdown inside the tr, you can also use element selector (i.e select) this:

Selected option value:

$(this).closest('tr').find('select').val();

Selected option text:

$(this).closest('tr').find('select option:selected').text();

Working Example

Upvotes: 0

chandu
chandu

Reputation: 2276

you can get the value of dropdown when the value changes. so use change method to get the value when option changes. JQuery Doc

$('input[type="select"]').change(function(){

var value = $( "select option:selected" ).text();
});

Update

you can just fetch the value by using the class also.

$(".selectboxClass").on('change',function(){
var value = $( ".selectboxClass option:selected" ).text();
});

Upvotes: 0

xei2k
xei2k

Reputation: 556

Depending on what you want to get, you can either get the value:

$("#statusprocess").val();

or the text of the selected

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

Upvotes: 0

Govind Singh
Govind Singh

Reputation: 15490

i think you can simply get it by

$("#statusprocess").val()

HTML

<select id='statusprocess' onChange="getIt()"><option value='optSelected'>Selected</option><option value='optNotSelected'>Not Selected</option></select>

SCRIPT

function getIt(){
var i=$("#statusprocess").val()
alert(i);
}

Upvotes: 0

mindbard
mindbard

Reputation: 38

Since you have the ID already, why don't you just get it via the ID?

$("#statusprocess").val();

The above statement returns 'optSelected' or 'optNotSelected'.

Or else if you were referring to the "option text" instead of the "option value", you can try this.

$("#statusprocess :selected").text();

The statement above returns 'Selected' or 'Not Selected'.

Upvotes: 1

Related Questions