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: Difference between revisions

From MoodleDocs
Created page with "== 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. Actually M..."
 
No edit summary
Line 14: Line 14:


//Your delete link
//Your delete link
$delete_link = new moodle_url('/delete_handler.php', array('id' => $id, 'action' => 'delete'), array('class'=>'delete_item', 'title' => 'Delete this'));
$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');
echo html_writer::link($delete_link, 'Delete this');


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

Revision as of 15:25, 5 August 2011

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.

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.