See-Why.Net

  • Home
  • About
  • Projects

See why...

Hi! My name is Christine, I am Computer Science and Biology student at UBC. This blog will be about two of my interests: programming and crafting.
  • Archives

    • July 2011
    • September 2010
    • July 2010
    • June 2010
    • May 2010
  • Categories

    • Code Notebook (1)
    • CPSC430 (12)
    • Crafting (2)
    • Firefox Extension Development (1)
    • Programming (2)
    • Site Talk (1)
  • Tags

    blue shrink plastic origami BusTime Extensions svn subclipse form api jquery drupal 7 version control eclipse Firefox microsoft facebook introduction wordpress livejournal kantianism copyright social contract theory internet utilitarianism crafts earrings ethics
  • Search

Jul
13
2011

Catching Multiple Checked Radio Button Values in Drupal 7 with JQuery

Filed under Code Notebook, Programming

In Drupal 7, I had two sets of radio buttons on a form, which I needed to get the checked values of using Jquery. This seemed simple to accomplish using .val(), with this line from the Jquery documentation:

$('input:radio[name=bar]:checked').val(); // get the value from a set of radio buttons

Drupal’s Form API names radio button sets as “setName[radios]“, and Jquery needs “[" and "]” characters to be escaped with a double backslash, as mentioned on this post at the drupal forums. So I tried


var fooValue = $('input:radio[@name='foo\\[radios\\]']:checked").val();
var barValue = $('input:radio[@name='bar\\[radios\\]']:checked").val();

But these lines retrieved the checked value for the first set of radio buttons only. At this point, I tried a non-existing name, and still Jquery retrieved the same value.

After trying a few more tweaks that did not work, I found that getting the text of the labels for all radios would retrieve the checked value of each, in a string, delimited by a space. At last I found that this worked:

var allChecked = $(input:radio:checked + label").text.split(" ");
var fooValue = allChecked[0];
var barValue = allChecked[1];

which is weird and cumbersome but at least it works. Maybe the problem is because Drupal 7 depends on Jquery 1.5.2 and the current version is 1.6.2? No idea.

Tagged: drupal 7, form api, jquery

0 Comments


© Christine Y. 2010. Powered by Wordpress.