jQuery return the first match in a select list -


i want select first option in select list matches value, last match selected, so:

jquery('.edit-item').click(function(){    var id = jquery(this).attr('id');    var assignee = jquery('#assignee-'+id).text();    jquery('#edit-form-'+id+' select[name=item[responsible]]').val(assignee);     return false; });  <select name="item[responsible]">   <option value="*">anyone</option>   <option value="'+$user+'">me ('+$user+')</option>')    -- here loop groups --     <option value="">----------</option>     <option value="'+$group+'">'+$group+'</option>')     -- here loop usernames within $group     <option value="'+$username+'">'+$username+'</option>')     -- end loop --    -- end loop --  </select>') 

now if enduser assignee user selected in group in, , not second value 'me (user)', there way or need workaround?

in usecase assignee backenduser it's simply

<select name="item[responsible]">  <option value="*">anyone</option>   <option value="mister x" selected=selected>me (mister x)</option> // 1 needs selected   <option value="1">1</option>   <option value="a">a</option>   <option value="b">b</option>   <option value="2">2</option>   <option value="c">c</option>   <option value="d">d</option>   <option value="mister x">mister x</option> // 1 selected </select> 

ok, selected=selected instead of checked=checked, whichever jquery uses, not done manually.

so $("option[value='mister x']:first").attr("selected", "selected") trick, if there 1 single list, how work when there multiple forms unique ids edit-form-# ?

answer ryanulit:

jquery('#edit-form-'+id+' option[value='+assignee+']:first').attr("selected", "selected"); 

i want select first option in select list matches value...

either 1 of these should you. of course, can replace 'mister x' value want search for.

$("option[value='mister x']:first").attr("selected", true); 

or

$("option[value='mister x']:first").attr("selected", "selected"); 

edit

that possible. refer particular select element. change name of select item_responsible else recommended , give same text it's id. can this:

$("#item_responsible option[value='mister x']:first") instead of $("option[value='mister x']:first")

...where #item_responsible select:

<select id="item_responsible" name="item_responsible">      <!-- options here --> </select> 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -