Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Javascript Custom Popup: Difference between revisions

From MoodleDocs
No edit summary
Line 1: Line 1:
== Custom Popup Messages Using Javascript ==
== Custom Popup Messages Using Javascript ==


There are times when you want something more that the usual stuck-in-the-80s interface that seems to be the Moodle norm.
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.
Actually Moodle has some nice utility functions in /lib/javascript-static.js which can help.

Revision as of 16:53, 5 August 2011

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.