1

I would like to have a field in module webform that uses javascript f.e that

 <script type="text/javascript">

var geocoder, location1, location2, gDir;

function initialize() {
    geocoder = new GClientGeocoder();
    gDir = new GDirections();
    GEvent.addListener(gDir, "load", function() {
        var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
        var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
        document.getElementById('d1').value =+drivingDistanceKilometers;
        document.getElementById('twojadres').value = '' + location1.address;



    });
}

function showLocation() {
    geocoder.getLocations(document.forms[0].address1.value, function (response) {
        if (!response || response.Status.code != 200)
        {
            alert("Przykro nam, ale wpisałeś niepoprawny adres. Spróbuj ponownie!");
        }
        else
        {
            location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
            geocoder.getLocations(document.forms[0].address2.value, function (response) {
                if (!response || response.Status.code != 200)
                {
                    alert("Sorry, we were unable to geocode the second address");
                }
                else
                {
                    location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
                    gDir.load('from: ' + location1.address + ' to: ' + location2.address);
                }
            });
        }
    });
}

</script>

It use js to calculate distance between address which should be get from webform field.

  • any specific js can be added in your content tpl file – Reza Shadman Jun 30 '13 at 13:06
2

An approach is to use hook_form_alter to alter the webform form and attach the custom JS.

/**
 * Implements hook_form_alter().
 */
function custommodule_form_alter(&$form, &$form_state, $form_id) {

  if ($form_id == 'webformCustomID')) {

    $form['#attached']['js'][] = drupal_get_path('module', 'custommodule') . '/custom.js';
  }
}

More info:

| improve this answer | |

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.