0

I have the following select list inside my web page:-

<select id="OrderStatus_12192838383111121" title="Status Required Field" class="ms-RadioText">
<option>In Progress</option>
<option>Waiting Customer Approval</option>
<option>Reject</option>
</select>

now i want to select one of the above options based on the value from another field, so i tried the following:-

var pmname = $('[id^="OrderProjectManagerStatus_"][id$="Display"]').attr("title");
alert(pmname);
$('select[id^="OrderStatus_"]').text() == pmname;

now the alert showed the correct value, which as "Reject", but this option did not get selected inside my select list using $('select[id^="OrderStatus_"]').text() == pmname;. so can anyone advice how i can dynamically set the selected option for my select list using jQuery ??

Thanks

John John
  • 1
  • 72
  • 238
  • 501
  • 1
    You can't assign a value to a method and `=` is assignment. You are meaning to use `.text(value)` or `.val(value)`. – Spencer Wieczorek Feb 14 '18 at 03:42
  • 2
    Possible duplicate of [Set select option 'selected', by value](https://stackoverflow.com/questions/13343566/set-select-option-selected-by-value) – Spencer Wieczorek Feb 14 '18 at 03:44
  • @SpencerWieczorek ok i tried this `$('select[id^="OrderAssignToApprover_"]').text(pmname)` but this result in having empty select list with no options.. – John John Feb 14 '18 at 03:50
  • @SpencerWieczorek not duplicate, and i already read the above question,,, where in the question they have the select list with options having value.. while in my case the options do not have values.. – John John Feb 14 '18 at 03:51
  • 1
    That doesn't matter, [`.val(value)` will still work fine](https://jsfiddle.net/jnm29oju/2/). Also I mean you are using `.text()` and `==` wrong in my first comment, and that you are intending to do `.text(value)`. That said that way doesn't work. – Spencer Wieczorek Feb 14 '18 at 03:57
  • @SpencerWieczorek yes you are correct `$('select[id^="OrderAssignToApprover_"]').val(pmname);` fix the problem – John John Feb 14 '18 at 03:59

2 Answers2

0

For selects you'll have to get the option element and set selected attribute to true

$('select[id^="OrderStatus_"] option').filter(function(index, item) { return $(item).text().trim() === pmname }).attr("selected", true);

And also, don't forget to have a value attribute assigned to option like so: <option value="Something">Something<option>

Varinder
  • 2,634
  • 1
  • 17
  • 20
  • i tried your code , but this result in having empty select list without any options.. now for having the select list with values, i do not own the markup, as this is a third party tool (sharepoint) and i can only write custom scripts again it.. – John John Feb 14 '18 at 03:54
  • it is likely that above piece of code is unable to find the ```option``` tag which matches ```pmname``` – Varinder Feb 14 '18 at 03:56
0

can look below may be help you understand

$( "[id^="OrderProjectManagerStatus_"] option:selected" ).each(function() {

   str += $( this ).text();
 });
Regolith
  • 2,944
  • 9
  • 33
  • 50
Wara Haii
  • 202
  • 2
  • 5