jQuery/CoffeeScript: hide div if all radios are false, but show if any is true -
i have div
need shown if user answers yes (clicks "true" radio) of 4 questions. if answer false all, hide div
. i've been having trouble if user changes his/her mind , goes , chooses other option on question. i've written coffeescript method handle it:
$ -> numberfalse = 0 numbertrue = 0 hideifallno = -> $('.legal-information').find('input[type=radio]').each -> if $(this).is(':checked') , $(this).val() 'false' if numberfalse >= 4 return else numberfalse += 1 if numbertrue >= 1 numbertrue -= 1 else numbertrue = 0 else if $(this).is(':checked') , $(this).val() 'true' if numbertrue >= 4 return else numbertrue += 1 if numberfalse >= 1 numberfalse -= 1 else numberfalse = 0 if numberfalse 4 $('.legal-info-extra-info').slideup('fast') if numbertrue >= 1 $('.legal-info-extra-info').slidedown('fast') $('.legal-information input[type=radio]').on 'change', -> hideifallno()
the issue here (other potentially questionable style choices; feel free weigh in on those, too) user can increment numberfalse
or numbertrue
clicking , forth on same 2 radio buttons single question. @ point, or if go , make different selections on multiple questions, counts messed up. had considered appending elements array , counting array length, got messy. how should handle user changing his/her mind?
edit
i've tried store true , false values in attributes on object, calling $('.legal-information').find('input[type=radio]:checked').attr('value','true');
sets value
true
. there way array of true
attrs?
does these local variables holding amount of true , false radios used elsewhere? if no, can skip sum , show div on true
checked this:
$ -> $('.legal-information input[type=radio]').on 'change', -> if $('.legal-information input[type=radio][value=true]:checked').length > 0 $('.legal-info-extra-info:hidden').slidedown('fast') else $('.legal-info-extra-info:visible').slideup('fast')
Comments
Post a Comment