Add Onclick Events to Drupal Menu Links
We’ve written before that menu links are one of the more basic features on a Drupal site.
One of our members wanted greater control over their menu links by using Javascript’s Onclick events.
An Onclick event allows you to choose the action that happens when someone clicks a link.
I’m going to show you an easy way to make that possible, without any custom coding.
Step #1. Installation
- Install and enable the Menu attributes module.
- Add this patch from the Menu attributes issue queue. If you’re not sure how to add patches, use the Patch Manager module. Your patch settings will look like this:
Step #2. Using Onclick Events
- Go to Structure > Menus and find the menu links you want to modify.
- Scroll down far enough and you will see an “Onclick” box. Here you can safely enter your Onclick events. These will be automatically added to your menu links on the frontend of the site. As shown in the image below, you don’t need to include the Onclick message. Here’s the original code: onclick=”alert(‘You are clicking on me’);”
Step #3. Testing Onclick Events
- Go to your menu and check your links. They should now have Onclick events working: