dynamic drop down menu get the wrong value, when calling onchange function

I have a dynamic dropdown menu on my wordpress site and I want to apply google analytic code so I can track when user click on any of the option and show the name of the value (not the id!). But now when I call the function using onchange, It will pass me the last value name from last dropdown menu. The <?php echo $value['value_name'] ?> can give me the right name and create the menu for me, but when I use the same code inside of the onchange function, the correct value didn’t pass to the function! I don’t quite get why I got such result and no clue about the solution. Thank you for advance of any kind of help and answer.

Here is the PHP code:

Read More
   <?php if ( isset( $optin_filter_manager[ $group['filter_id'] ]['view_filtre']       ) &&  $optin_filter_manager[ $group['filter_id'] ]['view_filtre'] == 'selected' ):?>
     <li>
       <!-- Problem Here  -->
       <select name="filter_value[<?php echo $group['filter_id']?>]" onchange="ga_call('<?php echo $value[value_name]?>')">
         <?php if (  isset( $optin_filter_manager[ $group['filter_id'] ]['all_field_enable'] ) &&  $optin_filter_manager[ $group['filter_id'] ]['all_field_enable'] == 'yes'  ):?>
           <option value="all">
              <?php echo ( empty ( $optin_filter_manager[ $group['filter_id'] ]['all_field_label'] ) )?'All':$optin_filter_manager[ $group['filter_id'] ]['all_field_label'];?>                                                         
           </option>
        <?php endif;?>
    <?php endif;

    foreach( (array)$group['value'] as $value  ):
    // generate catgory filter link

    $param['filter'] = $value['filter_value_id'];

    if ( isset( $optin_filter_manager[ $group['filter_id'] ]['view_filtre'] ) &&  $optin_filter_manager[ $group['filter_id'] ]['view_filtre'] == 'selected' ):?>
        <option name="<?php echo $value['value_name'] ?>" value="<?php echo $value['filter_value_id']?>" <?php selected( in_array( $value['filter_value_id'], (array) $filters ) ); ?> 
        <!-- get variable's name -->
        <?php echo $value['value_name'];?>
        </option>
    <?php else:?>

Here is my Javascript code for google analytics call:

function ga_call(value){
    ga('send', 'event', 'search', 'Filter',value);
}

======================================================

This is the follow up of the question. Thank you for caspian’s answer and here is my modified code:

<select name="filter_value[<?php echo $group['filter_id']?>]" onchange="ga_call(this.options[this.selectedIndex].text);" >

Related posts

Leave a Reply

1 comment

  1. php is server side – javascript is client side. When a browser goes to the page, it will render

    <select name="filter_value[<?php echo $group['filter_id']?>]" onchange="ga_call('<?php echo $value[value_name]?>')">
    

    as

    <select name="filter_value[<?php echo $group['filter_id']?>]" onchange="ga_call('current_value')">
    

    Get the current value of the dropdown:

    <select name="filter_value[<?php echo $group['filter_id']?>]" onchange="ga_call(this.value)">    
    

    Get the text of the selected item of a dropdown

    <select name="filter_value[<?php echo $group['filter_id']?>]" onchange="ga_call(this.options[this.selectedIndex].text);">    
    

    js fiddle demonstrating this: http://jsfiddle.net/WctY2/2/