0

The Question

How can I make a hidden div (triggered by radio button) show/hide for people that click it or tab through it in all browsers (Safari is my problem).

The Story

I have an html form with hidden div's scattered throughout to hide questions that might not need to be asked in some scenarios. Most of the hidden div's are triggered by radio buttons, so I decided to build the following script. I used .on("focus") so the hidden fields will work for people tabbing through the form.

$("#loan-application").on("focus","input.toggler,span.toggler", function(){
  var $show = ($(this).data("show")) ? $($(this).data("show")) : false,
      $hide = ($(this).data("hide")) ? $($(this).data("hide")) : false;
  if($show){    
    $show.stop(true).show({animation: "blind", duration: 1000}).addClass("active");
    if($show.data("validate")) $show.find(":visible:input.mandatory").addClass("required");
  }
  if($hide){
    $hide.not($show).each(function(){
      var $this = $(this);
      $this.stop(true).hide({animation: "blind", duration: 1000}).removeClass("active");
      ($this.data("validate")) ? $this.find(":input.mandatory").removeClass("required") : $this.find(":input.mandatory").addClass("required");
      resetForm($this);
    });
  }    
});

This code accomplishes the task in all browsers except Safari. If the radio button is clicked in Safari, it does not "count as focus" and the hidden field is not shown. So I tried switching the event to fire .on("click focus") which kind of works; however, if there are additional hidden divs within a previously hidden div, the content of the additional divs will expand outside of the parent into other text because the parents height does not change with the additional hidden divs.

Neve12ende12
  • 1,154
  • 4
  • 17
  • 33

1 Answers1

0

You can create click handler set focus.

Inside the click handler, you can use document.activeElement to determine if the element has focus already.

More information here: How do I find out which DOM element has the focus?

Community
  • 1
  • 1
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74