jquery

jQuery checkbox goodness

I was working on a select all feature today and just wanted to share this with the world. (a.k.a. keep it for later)


$(document).ready( function() {
$('a#select_all').click( function() {
$('input:checkbox').each( function() {
this.checked = !this.checked;
});
return false;
});
});

The first item to note is that jQuery created pseudo classes for input.

$('input:checkbox');
...
$('input:textbox');
... etc.

The second item to note, which I thought was ingenious, was setting the value of the checkbox to the inverse of it's current value.

this.checked = !this.checked;

Works great and it's short and sweet. Who loves jQuery?

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();

});

Syndicate content