Created
January 25, 2014 08:45
-
-
Save jakl/8613573 to your computer and use it in GitHub Desktop.
Collapsing radio buttons, rapid hack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<head> | |
<link rel="stylesheet" type="text/css" href="radios.css"> | |
</head> | |
<body> | |
<div class=radios></div> | |
<script src='https://code.jquery.com/jquery-2.1.0.min.js'></script> | |
<script src='./radios.js'></script> | |
</body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.inner { | |
padding-left: 40px; | |
} | |
.outter { | |
font-size: 30px; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function(){ | |
addRadios(radioData(), 'myRadios') | |
animateRadios() | |
}) | |
function animateRadios(){ | |
$('.inner').hide() | |
$('.outter').click(function(el){ | |
outter = el.target.parentNode.parentNode | |
if(el.target.checked && outter.className == 'outter'){ | |
$('.inner', outter).show() | |
others = $('.inner', $('.outter').not(outter)) | |
others.hide() | |
$('input', others).attr('checked', false) | |
} | |
}) | |
} | |
function addRadios(data, name){ | |
data.forEach(function(outterSet){ | |
innerSet = outterSet.splice(1)[0] | |
outter = outterSet[0] | |
markup = '<div class=outter><label><input type=radio name='+name+' value='+outter.value+'>' + outter.msg + '</label>' | |
innerSet.forEach(function(inner){ | |
markup += '<div class=inner><label><input type=radio name='+outter.childName+' value='+inner.value+'>' + inner.msg + '</label></div>' | |
}) | |
$('.radios').append(markup + '</div>') | |
}) | |
} | |
function radioData(){ | |
return [ | |
[{ msg: 'I need help with twitter facebook', childName: 'a', value: 'a' }, [ | |
{msg: "Can't continue", value: '1'}, | |
{msg: 'Never got email', value: '2'}, | |
{msg: 'I think its ok', value: '3'}, | |
{msg: 'Ignore me', value: '4'} | |
]], | |
[{ msg: 'I need help with twitter mobile', childName: 'b', value: 'b' }, [ | |
{msg: "Y it no work!", value: '1'}, | |
{msg: 'Everything broke', value: '2'} | |
]], | |
[{ msg: 'I need help with twitter email', childName: 'c', value: 'c' }, [ | |
{msg: "Nope", value: '1'}, | |
{msg: 'Yuuup', value: '2'}, | |
{msg: 'Maybe', value: '3'} | |
]], | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment