[support] [Fwd: Re: How to add "selected" attribute to select list <option>tag]

Greg Holsclaw Greg.Holsclaw at trouvemedia.com
Wed Nov 28 17:01:51 UTC 2007


I have noticed with the default values that you need to leave the form
page and return to get it to load the new default values. A refresh of
the screen won't work. I believe it is how the Forms API functions, only
looking for the default values for the initial form display, and any
reload after that it is looking for values from a previous POST.

 

Greg

 

________________________________

From: support-bounces at drupal.org [mailto:support-bounces at drupal.org] On
Behalf Of Sarah.Vardy at student.griffith.edu.au
Sent: Tuesday, November 27, 2007 9:26 PM
To: support at drupal.org
Subject: Re: [support] [Fwd: Re: How to add "selected" attribute to
select list <option>tag]

 

Hi Steve,

 

I just tested the code in firefox and it works. Maybe it is a browser
compliancy issue? 

 

Two of the names are displayed as selected initially.

 

Sarah
----- Original Message -----
From: Steve Edwards <killshot91 at comcast.net>
Date: Wednesday, November 28, 2007 3:19 pm
Subject: [support] [Fwd: Re: How to add "selected" attribute to select
list <option>tag]
To: support at drupal.org

> I guess I spoke too soon.  I did it as an array
> 
> #default_value => array(2,3)
> 
> and upon viewing the source, I discovered that it adds the 
> selected="selected" attribute to my option tags like I 
> want.  The 
> problem is, the items aren't being highlighted on the screen.
> 
> so this code:
> 
> *
>   * Define the project permissions settings form
>   */
> function project_permissions_form($project_nid, $project_name) {
>    $form['#base'] = 'project_permissions_form';
> 
>    $form[$project_nid] = array(
>      '#type' => 'fieldset',
>      '#title' => $project_name,
>      '#collapsible' => TRUE,
>      '#collapsed' => FALSE,
>    );
> 
>    $form[$project_nid]['projects'] = array(
>      '#type' => 'textfield',
>      '#title' => t('Project name'),
>      '#maxlength' => 30,
>      '#size' => 30,
>      '#default_value' => $project_name,
>    );
> 
>    $form[$project_nid]['users'] = array(
>      '#type' => 'select',
>      '#title' => t('Users with access to 
> project'),     '#description' => t('Select 
> users to be asssigned to the project'),
>      '#options' => _get_user_list(),
>      '#multiple' => TRUE,
>      '#default_value' => array(2,3),
>    );
> 
>    $form[$project_nid]['nid'] = array(
>      '#type' => 'hidden',
>      '#value'=> $project_nid,
>    );
> 
>    $form[$project_nid]['submit'] = array(
>      '#type' => 'submit',
>      '#value' => 'Submit'
>    );
>    return $form;
> }
> 
> 
> gives me this HTML:
> 
> <div class="form-item">
>   <label for="edit-users">Users with access to project: 
> </label>  <select name="users[]" 
> multiple="multiple"  class="form-select" id="edit-users" 
> ><option value="1">admin</option><option 
> value="2" selected="selected">bwald</option><option 
> value="3" selected="selected">testuser</option><option 
> value="4">Brian</option><option 
> value="5">NickH</option></select>
>   <div class="description">Select users to be asssigned 
> to the project</div>
> </div>
> 
> So why wouldn't the items be highlighted on the screen?
> 
> Steve
> 
> -------- Original Message --------
> Subject: Re: [support] How to add "selected" attribute to select 
> list <option>tagDate: Tue, 27 Nov 2007 21:04:32 -0800
> From: Steve Edwards <killshot91 at comcast.net>
> To: support at drupal.org
> References: <474CED98.8090201 at comcast.net> 
> <AF8E53B658278947AA3E09E48EF4EDAC3D73A3 at TRV1DMC00.TRV.local>
> Hmmm, getting closer, but I can't seem to get it to work.  
> No matter what I try, I can't get it to select anything.
> 
> Here is what my select list looks like (in HTML)
> 
> <option value="1">Name 1
> <option value="2">Name 2
> <option value="3">Name 3
> <option value="4">Name 4
> <option value="5">Name 5
> 
> The value is the user's uid. number That being the case, what 
> would i use for #default_value?  I've tried things like
> 
> '#default_value' => array(1 => 'Name 1')
> '#default_value' => 1
> '#default_value' => 'Name 1'
> 
> but I can't get any of them to work.  Any ideas?
> 
> Thanks.
> 
> Steve
> 
> 
> Greg Holsclaw wrote:
> > Make the #default_value an array of the keys of the options 
> you want
> > selected.
> > 
> > $form['item'] = array(
> > '#type' => 'select',
> > ...
> > '#options' => array of all options,
> > '#default_value' => array of keys from above options.
> > ...
> > );
> > 
> > Check
> > 
>
http://api.drupal.org/api/file/developer/topics/forms_api_reference.html
> /5#default_value for more info.
> > 
> > Greg
> > 
> > 
> > -----Original Message-----
> > From: support-bounces at drupal.org [mailto:support-
> bounces at drupal.org] On
> > Behalf Of Steve Edwards
> > Sent: Tuesday, November 27, 2007 8:25 PM
> > To: support at drupal.org
> > Subject: [support] How to add "selected" attribute to select list
> > <option>tag
> > 
> > Well, I'm back again, and this time I have a question about 
> attributes> for select options in a form.  I have a form 
> that has a 
> > select element (multi-select) that lists users.  The form 
> is for some
> > admin settings for a module.  When the form is displayed, 
> I 
> > need to be able to highlight specific options in the list 
> based on data
> > returned from a query.  I've been looking all day, and 
> > haven't been able to figure it out.  There is the 
> #attribute key, but it
> > is for the field as whole (at least as far as I can 
> > determine), and doesn't work for each individual <option> tag.
> > 
> > If anybody could point me in the right direction, I would 
> appreciate it.
> > 
> > Thanks.
> > 
> > Steve
> 
> -- 
> [ Drupal support list | http://lists.drupal.org/ ]
> 



Research Computing Services 
Sarah.Vardy at student.griffith.edu.au 
0402241794 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20071128/4a030b3e/attachment.htm 


More information about the support mailing list