Skip to content

Commit

Permalink
Fix radio show/hide behaviour outside a form (#375)
Browse files Browse the repository at this point in the history
The show/hide JS assumes radio buttons are inside a form, as the default behaviour of radio buttons is to scope their name to the nearest enclosing form. If they’re outside a form the JS fails. We can alter it to just select every applicable radio on the page in the absence of a form ancestor.

Adding tests for this involved quite a bit of rework as it turns out that Jasmine will just run every beforeEach() in order rather than letting beforeEach()s lower in scope override the parents.
  • Loading branch information
robinwhittleton committed Feb 1, 2017
1 parent 05c5d02 commit cf5efa4
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 193 deletions.
3 changes: 2 additions & 1 deletion javascripts/govuk/show-hide-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
function handleRadioContent ($control, $content) {
// All radios in this group which control content
var selector = selectors.radio + '[name=' + escapeElementName($control.attr('name')) + '][aria-controls]'
var $radios = $control.closest('form').find(selector)
var $form = $control.closest('form')
var $radios = $form.length ? $form.find(selector) : $(selector)

// Hide content for radios in group
$radios.each(function () {
Expand Down
Loading

0 comments on commit cf5efa4

Please sign in to comment.