There is some code that another developer repeated. I want to put that in a function and then call it but now I am a bit confuse.
This is the code I have:
if(true) {
$(this).attr('id', function (i, val) {
return val.replace('Less', 'More');
}).attr('data-bactmln', function (i, val) {
return val.replace('Less', 'More')
});
} else {
$(this).attr('id', function (i, val) {
return val.replace('More', 'Less');
}).attr('data-bactmln', function (i, val) {
return val.replace('More', 'Less')
});
}
And I would like to do something like this:
var toggleTealiumId = function(p1, p2) {
return val.replace(p1, p2);
};
if(true) {
$(this).attr('id', toggleTealiumId('More', 'Less'));
} else {
$(this).attr('data-bactmln', toggleTealiumId('Less', 'More'));
}
That val
is the value
of the string as in the first lines of code, but for now I am getting undefined
.
So how can I stop val
from being undefined
and do it in a proper way?
Any suggestions?