Monday 22 February 2016

Drupal 7 Jquery statement to add Class and remove when tabbing into an input

Here's a snippet of code that will allow you to change the class of an input field in your form when you tab into it.

I've got this in a file in my theme /sites/all/themes/mytheme/js/custom

Remember to add this file to the file /sites/all/themes/mytheme/mytheme.info

(function ($) {
Drupal.behaviors.skyFocus = {
attach: function (context, settings) {

var focusClass = "has-focus";

$('.form-text').keyup(function(e) {
if(e.keyCode === 9){
$(this).addClass(focusClass);
}
}
);
$('.form-text').focusout(function() {
$(this).removeClass(focusClass);
}
);

}
};
})(jQuery);

No comments: