For the short way, try this:
if ($(this).data("function") === 'blogFunctionaliteit') {
// TODO: enter your code...
} else if ($(this).data("function") === 'offerteFunctionaliteit') {
// TODO: enter your code...
} else if ($(this).data("function") === 'overzichtFunctionaliteit') {
// TODO: enter your code...
} else {
// do nothing
}
Meanwhile I've earned a lot of experience with JavaScript and I have some improvements for my own code suggestion. If anyone is reading this answer I would highly recommend to use the following code:
var self = this;
var $self = $(self);
var dataFunction = $self.attr('data-function');
switch (dataFunction) {
case 'blogFunctionaliteit':
// TODO: enter your code...
break;
case 'offerteFunctionaliteit':
// TODO: enter your code...
break;
case 'overzichtFunctionaliteit':
// TODO: enter your code...
break;
default:
// do nothing
break;
}
But why the hell you should use this? It's a lot more complicated! Well, yes it is, but here's why:
- You should always store
this
in a variable, becausethis
may not be what you think it is, checkout this link: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Operators/this - You should call the jQuery functions (
$(...)
) only once and then store it in a new variable to improve performance. The same for object functions. - For such cases it's better to use the switch-case construct, checkout this link: Case vs If Else If: Which is more efficient?
Trust me, you will have a less problems with JavaScript if you start writing your code like the second code snippet.