JQuery refresh text dropdown select

I am trying to update the text of one of the options in the drop-down list after the action on my page. Does anyone know how to do this in jquery? I tried this:

$("#selectid").text("newtext"); 

But this will remove all other options in the selection list and make it empty. I know this is the wrong approach because I just want to update one of the parameter values. thanks for the help

+3
source share
2 answers
$('#selectid option:eq(NUMERIC_INDEX_GOES_HERE)').text('newtext');

or

$('#selectid').find('option[value="OPTION_VALUE"]').text('newtext');

or

$('#selectid option').filter('[value="OPTION_VALUE"]').text('newtext');

or

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

And to change the value of a parameter, you can of course use:

$('#selectid option:eq(NUMERIC_INDEX_GOES_HERE)').val('new value goes here');
0
source

Source: https://habr.com/ru/post/1728064/


All Articles