1

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?

4
  • post your html. Is there only one select in the whole page? Commented Apr 4, 2017 at 2:10
  • what's the value of selectedField Commented Apr 4, 2017 at 2:11
  • there can be many selects and I do not know their ids. I will update my question to give you a bit more detail Commented Apr 4, 2017 at 2:12
  • question updated Commented Apr 4, 2017 at 2:14

2 Answers 2

1

You can chain your jQuery Do something like:

$('#basic_select_xxxx').find('select option:contains("OLD_TEXT_VALUE")').text('newtext');
Sign up to request clarification or add additional context in comments.

Comments

1

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.