views:

249

answers:

3

I want to select the first option in a select list which matches a certain value, currently the last match is selected, like 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 a loop with groups --
    <option value="">----------</option>
    <option value="'+$group+'">'+$group+'</option>')
    -- here a loop with all usernames within $group
    <option value="'+$username+'">'+$username+'</option>')
    -- end loop --
   -- end loop --
 </select>')

Now if the enduser is the assignee that user is selected in the group he is in, and not the second value 'me (user)', is there a way to do this or do I need some workaround?

In the usecase the assignee is the backenduser it's simply

<select name="item[responsible]">
 <option value="*">Anyone</option>
  <option value="mister x" selected=selected>Me (mister x)</option> // this one needs to be 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> // this is the one that is selected
</select>

Ok, so selected=selected instead of checked=checked, whichever jQuery uses, it is not done manually.

So $("option[value='mister x']:first").attr("selected", "selected") does the trick, if there would be one single list, how does this work when there are multiple forms with all their unique ids edit-form-# ?

answer by ryanulit:

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

my bad, I tried to add a comment, but couldn't use code tags

Cornelis
Cornelis, StackOverflow is **not** a forum. If you have information to add to your question, then edit your question.
Jonathan Sampson
A: 

In a select box checked="checked" is a common mistake, it should be selected="selected" on html part.

Sinan.

Sinan Y.
+2  A: 

I want to select the first option in a select list which matches a certain value...

Either one of these should do that for you. Of course, you can replace 'mister x' with any value you want to search for.

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

or

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

EDIT

That is possible. Just refer to the particular select element. Also I would change the name of your select to item_responsible like someone else recommended and also give it the same text for it's ID. Then you can just do this:

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

...where #item_responsible is your select:

<select id="item_responsible" name="item_responsible">
     <!-- options here -->
</select>
ryanulit
thanks, that is something to start with, it is possible to change this so i do not have to use the option-tag as this is changes all select boxes on a page
Cornelis
alright! thanks a lot, this worksthe name is not a problem, and the id cant be the same as there are multiple select boxes with the same options, not that I use the brackets, but if I recall it right PHP benefits from using [] when processing form data, I believe it is automatically put in an array 'item'. Sure the id's can be uniquefied but I do not use the select id's.
Cornelis
OK, well then to find the select you prob have to modify it to $("select[name='item[responsible]'] option[value...").
ryanulit