0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

optionをテキストで検索してselected状態にする

Last updated at Posted at 2021-03-09

経緯

jQueryでselectのoptionから選択したい。
その時の指定方法は、valueではなくテキストで指定したい。

結論

example.html
<select id="selector">
    <option value="" selected>-</option>
    <option value="1">あああああ</option>
    <option value="2">ららららら</option>
    <option value="3">ぶらぶらぶーらぶら</option>
    <option value="4">あべし</option>
    <option value="5">ひでぶ</option>
</select>
example.js
var blah = 'ぶらぶら'
$(`#selector :contains(${blah})`).prop('selected', true);

これで、value="3"のoptionがselectedされる。

解説

コンテントフィルター:containsを使う。
https://api.jquery.com/contains-selector/#contains1

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?