Mayank Patel
Mayank Patel

Reputation: 117

How to get value from select using jquery

Prefer this example so you have better idea for give answer

<select id="cmbfotos" onchange="aplicarFoto()">
    <option value="gokussj3.jpg">Value 1</option>
    <option value="gokussj4.jpg">Value 2</option>
    <option value="gohanssj2.jpg">Value 3</option>
    <option value="gotenks.jpg">Value 4</option>
    <option value="krilin.jpg">Value 5</option>
</select> 

In above ex. when i select two value like Value1 and value2 both value i require so how to i get it.

i not require value like "gokussj3.jpg" but Value 1

If some idea share me .....

Upvotes: 1

Views: 80

Answers (4)

user3614902
user3614902

Reputation:

Use this solution for your requirement

// Use for multiple select in select

 $('#cmbfotos  option').each(function() 
    {
        if($(this).val() == 'gokussj3.jpg') 
        {
            $(this).prop("selected", true);
        }
    });

// Get the value from a drop down select

$( "#cmbfotos option:selected").val();

// Get the value from a dropdown select even easier

$( "select.foo" ).val();

// Get the value from a checked check box

$( "input:checkbox:checked" ).val();

// Get the value from a set of radio buttons

$( "input:radio[name=bar]:checked" ).val();

Upvotes: 1

Gaurang s
Gaurang s

Reputation: 832

$('#cmbfotos option:selected').text(); ////is for text and
    $('#cmbfotos option:selected').val() ///is for value;

Upvotes: 1

Kiran
Kiran

Reputation: 20313

You should use jquery .change() for select onchange event, :selected to select all elements that are selected and .text() to get text from element. Try this:

$("#cmbfotos").change(function(){
    var text = $(this).find("option:selected").text();
    alert(text);
});

DEMO

Upvotes: 1

MrCode
MrCode

Reputation: 64526

Access the options, filtering by :selected, then get the .text().

Fiddle

$('#cmbfotos option:selected').text();

Upvotes: 2

Related Questions