I've read millions of similar posts on this but I just can't get my head around it. So far I understand that I need to use Drupal.attachBehaviors, and I've tried so many different approaches that I've seen but nothing works.

Basically I have a jQuery plugin that is styling any select boxes on the site, which works great, apart from on my views exposed form with ajax enabled. The jQuery is literally as simple as $('select').styledSelect(); after including the plugin itself.

EDIT: this is almost there:

      Drupal.behaviors.styledSelect = {
        attach: function() {

But now the ajax call seems to be looping.

EDIT again:

Well it seems to be something to do with the actual plugin, as other Javascript seems to work fine. I'm so close! Here is the plugin:

jQuery.fn.styledSelect = function(options) {
var isFF2 = jQuery.browser.mozilla && jQuery.browser.version.indexOf('1.8.')==0;
var prefs = {
    coverClass : 'select-replace-cover',
    innerClass : 'select-replace',
    adjustPosition : { top:0, left:0 },
    selectOpacity : 0
if (options) jQuery.extend(prefs,options);
return this.each( function() {
    if (isFF2) return false;
    var selElm = jQuery(this);
    var selReplace = selElm.next();
    var selCover = selElm.parent();

    selElm.bind('change',function() {
        }).bind('resize',function() {
        jQuery(this).parent().width( jQuery(this).width()+'px' );

EDIT again:

it was the trigger 'change' that was causing the problems - I've changed it to click, and it all seems good woohoo!

As ever, thanks for everyone's wonderful advice.

2 Answers 2


You'll need to wrap the JS in a Drupal behaviour, so that it is attached after the ajax refresh.

You should also ensure it only runs once

 Drupal.behaviors.styledSelect = {
   attach: function() {
     $('select').once('ss', function() {

That way it won't get 'styled' on every ajax change.


I do not like to use drupal behaviors in this instance as it does fire multiple times. Another option is to simply wait for the ajax to complete like this:


Ideally you want to replace document with the element that is getting fired via ajax. But this works as well but will fire on any ajax on the page (or globally if its loaded on every page)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.