Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have included a javascript in the module "myid" I've been developing in drupal by using this code:

function myid_init() {
    drupal_add_js(drupal_get_path("module", "myid") . "js/myid.js");   
}

This is my Javascript file "myid.js":

function myid_js_start(){
    alert("hello world");
}

Below is the code that creates a button that invokes an alert function:

$form['add_button'] = array(
    '#type' => 'button',
    '#value' => 'Take a picture',
    '#attributes' => array('onclick' => 'myid_js_start()'),    
);

I dont know where did I go wrong. The button shows up but doesn't fire any alert function. Can anyone help me with this?

share|improve this question
    
Standard question: have you cleared your cache? Have you tried specifying that the function is javascript as per stackoverflow.com/questions/21032365/… –  Darvanen Jan 12 at 4:05
    
I always clear the cache, everytime I make changes. –  XXX.xxx Jan 12 at 5:10
1  
Sadly there are too many who post here that do not know to do that, therefore it is helpful to include that fact in your question =) –  Darvanen Jan 12 at 5:21

2 Answers 2

Try to write JS in drupal way like this:

(function($) {

    Drupal.behaviors.xyzModule = {
        attach: function(context, settings) {


//            $('.test', context).click(function() {
//                $(this).next('ul').toggle('show');
//            });

//myid_js_start();
            function myid_js_start() {
                alert("hello world");
                //console.log(' Hello');
            }

        }
    };

})(jQuery);

For More info visit here

share|improve this answer

This:

drupal_add_js(drupal_get_path("module", "myid") . "js/myid.js");

Needs to be:

drupal_add_js(drupal_get_path("module", "myid") . "/js/myid.js");

Note the forward slash before the path to the js file

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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