niko craft
niko craft

Reputation: 2977

update text in select using jquery

I need to update an option text and I do not have #id of the select box.

I found examples like this:

$('#selectid option:contains("OLD_TEXT_VALUE")').text('newtext');

I can't use this example, since I can only access select like this:

selectedField.find('select')

how do I add

option:contains("OLD_TEXT_VALUE")').text('newtext');

to the above way of selecting select, is it possible?

This is my code, there can be many selects dynamically inserted into the page in this format.

<div id="basic_select_xxxx" class="form-group selectable">
    <select class="form-control">
        <option>Select something</option>
    </select>
</div>

I can select the select only like this:

$('#basic_select_xxxx').find('select).

how to then update select option with new text by looking up the old value? Is it possible this way? I can not put id on select, so let me know if there is other way?

Upvotes: 1

Views: 41

Answers (2)

charlietfl
charlietfl

Reputation: 171669

Assuming all the containers ID's start with same "basic_select_" prefix you can use that in an "attribute starts with" selector

$('.selectable[id^="basic_select_"] select option:contains("OLD_TEXT_VALUE")').text('newtext');

Upvotes: 1

Nishanth Matha
Nishanth Matha

Reputation: 6081

You can chain your jQuery Do something like:

$('#basic_select_xxxx').find('select option:contains("OLD_TEXT_VALUE")').text('newtext');

Upvotes: 1

Related Questions