I have a function that I would like to apply to different elements - or rather, different selectors creating different contexts. How can I modify the below function to use $('this'), instead of specifying the $('#themes') selector inside the getClass function?
The code:
$('#themes').change(function(e) {
getClass(this, e);
});
function getClass() {
var classToAdd = $('#themes').find('option[value]:selected').map(function() {
return this.value;
}).get().join(' ');
var allClassess = $('#themes').find('option[value]').map(function() {
return this.value ? this.value : null;
}).get().join(' ');
$('#result').removeClass(allClassess).addClass(classToAdd);
}
See fiddle here.
thispoints to the current element the event is fired for, so you shouldn't re-select the element by a selector, usethisinstead.