How to make menu element non clickable

Sometimes we need to make some menu items non clickable, there are some modules in drupal wich can make it , but installing module is not the easiest solution. Couple js lines given below is easiest way as i think. This way is the best when we have dropdown menu with a lot of items in different levels, only in this case our solution is very suitable. Here is code:

jQuery('#block-system-main-menu ul.menu li a').each(function() {
			if(jQuery(this).text() == 'Menu_1_item_text') jQuery(this).click(function() {
				return false;
			});
			if(jQuery(this).text() == 'Menu_2_item_text') jQuery(this).click(function() {
				return false;
			});
		});

in my case in drupal 7 menu we get each <a> elements inside <li> and check if his text value equal to those what we need. I mean if we need to make non clickable menu item with name 'Home' we have to use this code.

jQuery('#block-system-main-menu ul.menu li a').each(function() {
    if(jQuery(this).text() == 'Home')     jQuery(this).click(function() {
   return false;
	});

});

if text matches we make this element not clickable. In case of simple menu we can use something like this code.   

jQuery('#block-system-main-menu ul.menu li a').eq(number).click(function() {
    return false;
});

Where number is the menu item number if we count them starting from zero. For example if we need to make non clickable menu third menu item we should use this code.

jQuery('#block-system-main-menu ul.menu li a').eq(2).click(function() {
    return false;
});

So thats all. Continue doing drupal!

Rating: 
5.42857 out of 10 based on 35 ratings.