How to add JavaScript code to a webform?
The recommended way to add a JS to any form in Drupal is using #attached to the $form element.  

In order to add the JS to a webform, you would have to implement hook_form_alter or hook_form_FORM_ID_alter, and add the #attached

function MODULE_form_webform_client_form_3_alter(&amp;$form, &amp;$form_state) {
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'MODULE') . '/MODULE.js',
  );
}


Then create the MODULE.js file inside the module directory, and write your JS there.

Refer to The Drupal JavaScript API, to see how to work with JS in Drupal way.