Is it possible to add java script to webform? I would like to have a field in module webform that uses javascript f.e that

 &lt;script type="text/javascript"&gt;

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);
                }
            });
        }
    });
}

&lt;/script&gt;


It use js to calculate distance between address which should be get from webform field.
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(&amp;$form, &amp;$form_state, $form_id) {

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

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


More info:


https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_alter/7
https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_FORM_ID_alter/7
Adding CSS and JS to form with attachments