I have the code below in my own drupal module(myid):
myid.module file:
function myid_init() { drupal_add_js(drupal_get_path("module", "myid") . "/js/myid.js"); } function myid_menu() { $items = array(); $items['admin/settings/myid'] = array( 'title' => 'myid Settings', 'description' => 'Configuration options for the myid module', 'page callback' => 'myid_admin_settings', 'access callback ' => 'user_access', 'access arguments' => array('administer site configuration'), ); return $items; } function myid_admin_settings() { return drupal_get_form('myid_settings_form'); } function myid_settings_form() { $form = array(); $form['settings'] = array( '#type' => 'item', '#title' => t("Registrar's Signature"), '#description' => t(''), '#markup' => "<canvas id='myid_registrar_signature' width='500' height='100'></canvas>", '#required' => TRUE, ); return $form; }
myid.js file:
1. $( document ).ready(function() { 2. myid_retrieve_save_information(); 3. }); 4. function myid_retrieve_save_information(){ 5. var cnv_rsignature = document.getElementById("myid_registrar_signature"); 6. var ctx_rsignature= cnv_rsignature.getContext('2d'); 7. }
Why does every time I load the page, it gets the following error pointing to Line 6 in myid.js:
Uncaught TypeError: Cannot read property 'getContext' of null
Is it because #myid_registrar_signature was not yet loaded when Line 6 was executed? Where did I went wrong?
Sponsored by SupremePR