Note:

This site is no longer used and is in read-only mode. Instead please go to our new Moodle Developer Resource site.

Javascript Custom Popup

From MoodleDocs

Custom Popup Messages Using Javascript

There are times when you want something more that the usual Moodle interface.

Actually Moodle has some nice utility functions in /lib/javascript-static.js which can help.

Try this to make custom confirm messages.

// Required JavaScript code. $PAGE->requires->js('/mod/yourmode/your_js_file.js');

....

//Your delete link $delete_link = new moodle_url('/delete_handler.php',

       array('id' => $id, 'action' => 'delete'), 
       array('class'=>'delete_item', 'title' => 'Delete this'));

echo html_writer::link($delete_link, 'Delete this');

And the Javascript: YUI().use('node-base', function(Y) {

   function init() {
       Y.all(".delete_item").on('click', function(e) {
           var args = {'url':e.currentTarget.get('href'),
               'message':'Are you sure you want to delete ' + 
                 e.currentTarget.get('title') + 
                    ' ? All fields and data associated with this form will be lost'};
           M.util.show_confirm_dialog(e, args);
           return false;
       });
   }
Y.on("domready", init);


});

The code will pick up the url in the link and use the text in the title attibute to create a custom confirm message.