[development] I am trying to use the hook_form_alter to change the option that is selected by default within a select pull-down when the page loads.

John Mitchell mitchelljj98 at gmail.com
Fri Mar 25 17:53:12 UTC 2011


I am trying to use the hook_form_alter to change the option that is selected
by default within a select pull-down when the page loads.  I am able to
adjust the order of the options before the page loads but changing the
#default_value option does not change the actual selected option when the
page loads.  Note function *product_type_form_alter *below:

*function product_type_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
    $form['term_node_tid_depth']['#default_value'] = 184;//this did not work
    $form['term_node_tid_depth']['#options'] = Array(193 => "Alpena County",
184 => "Genesee County", 190 => "Montmorency County", 186 => "Oceana
County", 204 => "Wabasha County", "All" => "<Any>");//this worked
    $form['term_node_tid_depth']['#default_value'] = 184;//this did not work
    dd($form['term_node_tid_depth']['#default_value']);//the outputted log
value is correctly as 184 but when the page loads it uses the original
default of "All"
  }
}*

If I instead include a javascript file within hook_form_alter (*listed below
*) then I am able to change the actual selected option after the page loads
and programmatically click the submit button to rerun the filter, but this
is needless processing that causes the page to load twice.  How can I use
the method above to change the actual selected option before the page loads?

Thanks,

John

*function product_type_form_alter(&$form, &$form_state, $form_id) {//this
worked completely
  if ($form_id == 'views_exposed_form') {
    $term_node_tid_depth_get = $_GET['term_node_tid_depth'];
    if (!isset($term_node_tid_depth_get)) {
      drupal_add_js(drupal_get_path('module', 'product_type') .
'/js/subscription_product_filter.js','module','footer');
    }
  }

'/js/subscription_product_filter.js' contents below:
jQuery("select#edit-term-node-tid-depth
option[selected]").removeAttr("selected");
jQuery("select#edit-term-node-tid-depth
option[value='184']").attr("selected", "selected");
$("#edit-submit-Subscription-Products").click();*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20110325/fe2f6a49/attachment.html 


More information about the development mailing list