Joseph
Joseph

Reputation: 348

jquery how to find and replace a selected option that has a certain value

Is there any way to find in a select the selected option that has a certain value and replace it ?

<div id="l1col1" class="selects1" ></div>
<div id="l1col2" class="selects1" ><select><option selected>2</option></select></div>
<div id="l1col3" class="selects1" ><select><option selected>1</option></select></div> 

I will be more clear, in the example above i want to find the select that has the option selected as 1 and replace it with 3 , is this possible ?

Upvotes: 1

Views: 646

Answers (3)

dreamweiver
dreamweiver

Reputation: 6002

try this way

    $('select').on('change', function () {
       if ($(this).find('option:selected').text() == '1') 
            $(this).find('option:selected').text('3');
    });

Live Demo:

http://jsfiddle.net/dreamweiver/5zA4A/12/

Happy Coding :)

Upvotes: 0

Anton
Anton

Reputation: 32581

You can use :contains

$('.selects1 :selected:contains("1")').text('3');

DEMO

Upvotes: 2

tymeJV
tymeJV

Reputation: 104785

You can do:

$("select").each(function() {
    var option = $(this).find("option:selected");
    if (option.text() == "1")
        //do stuff
});

Upvotes: 1

Related Questions