Skip to content

Instantly share code, notes, and snippets.

@davebowker
Last active January 18, 2017 11:25
Show Gist options
  • Select an option

  • Save davebowker/c52d37aba303f3edbced292cd3b010b1 to your computer and use it in GitHub Desktop.

Select an option

Save davebowker/c52d37aba303f3edbced292cd3b010b1 to your computer and use it in GitHub Desktop.
/*
* Set interaction class on body
*/
$(function () {
$('input, textarea').focus(function() {
// ID
var action_id = $(this).attr('id');
if (action_id) {
$('body').data('action_id', 'action-' + action_id)
.addClass($('body').data('action_id'));
}
// Class(es)
var class_id = $(this).attr('class');
if (class_id) {
$('body').data('action_class', class_id.split(' '));
var action_class = $('body').data('action_class');
for (var i = 0; i < action_class.length; i++) {
$('body').addClass('action-' + action_class[i]);
}
}
}).blur(function() {
// ID
if ($('body').data('action_id')) {
$('body').removeClass($('body').data('action_id'))
.removeData('action_id');
}
// Class(es)
if ($('body').data('action_class')) {
var action_class = $('body').data('action_class');
for (var i = 0; i < action_class.length; i++) {
$('body').removeClass('action-' + action_class[i]).removeData('action_class');
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment