0

I'm pretty new with Drupal & JQuery. I post this question because I was not able to find a solution in the net.

I have a select form element to choose a country. This select displays de country names but keeps the country_code because this is the value to be stored in the DDBB once the form submitted. This select element is linked to ajax because there is a second select field which should show values depending on the selected country.

As soon as the complete address is introduced, JQuery should show a map with the location of the entered address. For that reason I need JQuery to access the country name before the form is submitted.

There is a function that returns the country name for a given country_code but I do not know how to run that function so that the result can be catched by JQuery. One solution would be keep the country_name instead of the country_code (in the select element) but then , into the submit function of the form, I would need a function to get the country code and I would like to avoid this.

$country_list=get_country_list();

$form['c_pais'] = array(
'#title' => t('Address - Country'),
'#type' => 'select',
'#options' => $country_list,
'#required' => TRUE, 
'#default_value' => $value_country,
'#ajax' => array(
    'event' => 'change',
    'callback' => 'call_region1_select',
    'wrapper' => 'dropdown_region1',
),
);


function get_country_list()
{
    db_set_active('mydb');
    $result=db_select('country_list')
    ->fields('country_list',array('country_id','country_name')) 
    ->orderBy('country_name','ASC')
    ->execute();
    $llista_paisos['0']='--- chosse a country ----';
    while($node = $result->fetchAssoc()) 
    {
        $llista_paisos[$node['country_id']]=$node['country_name'];
    }
    db_set_active();
    //print_r($llista_paisos);
    return $llista_paisos;
}
1

Use like this to get form select list element in jQuery.

$("#elementId :selected").text() //the text content of the selected option(country name)
$("#elementId").val() //the value of the selected option(country code)
1
  • this solution did not work but helped me to find the correct one in my case. Some else asked similar question and solution given was $("#yourdropdownid option:selected").text(); and this ones wrorks for me. many thanks – David_A Sep 25 '15 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.