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
Revision as of 13:39, 14 July 2021 by David Mudrak (talk | contribs) (Text replacement - "<code php>" to "<syntaxhighlight lang="php">")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Custom Popup Messages Using Javascript

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

The manual way

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 <b>' + 
                  e.currentTarget.get('title') + 
                     '</b> ? 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.

The easy way

See, for example, https://github.com/moodle/moodle/blob/master/mod/workshop/view.php#L389