jQuery: working with select options

Following my recent post on my rolechanger module, I started delving into how to provide functionality with the resulting dropdown box that the module provides. I wanted to provide all functionality through javascript and have included a rolechanger.js file for this purpose. However I found a few quirks while working with jQuery and the select element.

I started out with a .change() on the select element and drilled down through the .children() looking for [@selected]. However, thought this worked fine in IE, Firefox did not pick this up at all. I saw in the jQuery issues that this had been fixed, but evidently not for the release that ships with Drupal. Here’s the resulting code for the IE portion:

    // ie doesn't pick up the option.click, so we'll have to use the select.change
    $('#edit-role-changer').change(function(){

  	// get rid of select box
  	$('#edit-role-changer').hide();

        // show the chosen role as static text
       // NOTE: FF doesn't pick up [@selected]
      $('#role').html($(this).children("[@selected]").text()).show();

      // provide a change link
      $('#role-change-back').show();

    });

Firefox was actually a little easier to code for, since I found that it would pick up a .click() on the option element. So the resulting code for Firefox is this:

    // provide action for an option change
    $('#edit-role-changer option').click(function(){

  	  // get rid of select box
  	  $('#edit-role-changer').hide();

  	  // show the chosen role as static text
      $('#role').html($(this).text()).show();

      // provide a change link
      $('#role-change-back').show();

    });

Since Firefox does pick up the .change() function that is on the select element that is used for IE, I then had to do a browsers detection, which jQuery makes simple as pie with $.browser. Here is the resulting full code:

  // see which browser we're working with
  if ($.browser.msie) {

    // ie doesn't pick up the option.click, so we'll have to use the select.change
    $('#edit-role-changer').change(function(){

  	  // get rid of select box
  	  $('#edit-role-changer').hide();

  	  // show the chosen role as static text
  	  // NOTE: FF doesn't pick up [@selected]
      $('#role').html($(this).children("[@selected]").text()).show();

      // provide a change link
      $('#role-change-back').show();

    });
  }
  else {

    // provide action for an option change
    $('#edit-role-changer option').click(function(){

  	  // get rid of select box
  	  $('#edit-role-changer').hide();

  	  // show the chosen role as static text
      $('#role').html($(this).text()).show();

      // provide a change link
      $('#role-change-back').show();

    });
  }

Happy jQuerying!

read on

Hey, you gotta start somewhere right?

So my first commit to the Drupal contributions has been made! WooHoo! I partied hard that night :) No, it’s really not that big of a deal, but I’m excited as it takes me one step further into Drupal development and being more involved in the Drupal community.

read on

Marti Gras Weekend

So Marti Gras weekend was great! I’m so fraking sore form Wii that it’s not even funny. Well maybe a little bit funny… Snugs and his wife were in town as well as Dave, so we all hung out at Dana’s, drank, and played Wii for 8 hours straight! It was a blast, a good time was had by all. This video is of Dave and Dana boxing, but we bowled a hell of a lot more. We almost all hit pro by the end of the night (3am). Too bad for Amy, who hurt her leg earlier in the day and was not able to bowl very much, but she did pretty well sitting down, even on her Vikoden! We also played with my Mac’s PhotoBooth program, hilarity ensued. </param></param></embed>

read on

Drupal Theme Loader

I’ve started a project on drupal.org to contribute a module that will allow loading of new themes into Drupal from a compressed file (zip, gzip) that is either on your local hard drive, or from a web URL (curl support required?). View Project on drupal.org

It’s a basic idea and I’m hoping to get some feedback from the Drupal community on how best to execute this. The idea came about when a friend of mine mentioned that he had introduced his brother (a Joomla user) to Drupal and got some feedback from him on what he thought Drupal was lacking.

The first thing mentioned was the weight system. Joomla uses a system similar to that which is implemented in the Views module where you can hit and up or down arrow to resort content. I heard mention that someone has been working on a module to do sortables with Drupal’s new jQuery library, but it looks to be a 4.7 implementation so far.

I’ve not really seen much discussion on drupal.org about a theme installation interface, so I figure since it would help convert users and there is some good interest at least outside the drupal community, it may take some hold.

Update: Due to more commitments then I have time, I’ve discontinued any work on this module. My regrets.

read on