How to disable button based on route?

Hi Commune,

I would like to ask if how do you disable button based on route?

My use case is to have a button disabled if on the create route but if it’s on update route it’s enabled.

Thanks

You can use an if statement like this:

//HTML {{#if path 'create'}} <button>Create</button> {{/if}}

The helper on the .js file would be someling like:
//JS

Template.index.helpers({ path: function(path_name) { if (window.location.pathname === path_name) { return true; //Instead of true you can return "disabled" and put the helper in the button's class } } });

This will remove directly the button if the window.location.pathname is different from the one that you put in the html template.
If you want to disable the button using the css use the same approach but instead of an “if statement” in the template just put the helper inside the button’s class attribute and make the helper return “disabled”(this should work, hadn’t got the possibility to test it):
http://www.w3schools.com/tags/att_button_disabled.asp

Probably there are better approaches! I’m waiting for the answers of ther users :slightly_smiling:

Hope this will help you

1 Like

Thanks @0cn421 for the help! :+1:

I overlooked the path keyword, it is fixed now.

1 Like