-1

I need to check if an option(arrayItem) already exists in my selectpicker to populate it with :

$("#iselectroledirregional").append('<option value="' + arrayItem + '">' + arrayItem + '</option>').selectpicker('refresh');

I already tried this in an if/else statement:

$("#yourSelect option[value='yourValue']").length > 0;

But it doesn't work because my select dropdown does no contains a value tag :

enter image description here

Thank you

userHG
  • 567
  • 4
  • 29

1 Answers1

0

You can set a specific class on each label span like this:

<span class=text val-NR">NR<span>

Then you have the possibility to check it with this code:

$("#yourSelect a[role='option'] > span.val-NR").length > 0;

Another way is to use the jquery contains pseudo class:

$("#yourSelect a[role='option'] > span:contains('NR')").length > 0;

You can find the contains selector here: https://api.jquery.com/contains-selector/. If this not works fine, then give us the full html and js code.

Marc
  • 1,836
  • 1
  • 10
  • 14
  • I cannot use the first one but your third seems to be good, I tried it but I never have a true on this condition – userHG May 20 '20 at 09:45
  • try to make the selector more short: $("#yourSelect span:contains('NR')").length > 0; – Marc May 20 '20 at 09:48
  • You can also try to execute this code in the developer web console of your browser: $("#yourSelect span:contains('NR')").length – Marc May 20 '20 at 09:51
  • I always get false – userHG May 20 '20 at 10:03
  • Here is how my HTML is rendered. I can't even see my two options in the dropdown http://jsfiddle.net/decodeman/v7naao59/ – userHG May 20 '20 at 11:21