fegemo
fegemo

Reputation: 2594

How to check if the focus left a group of elements

I want to have a form with 4 inputs that typically displays only the first one. When it gets focus, the other 3 inputs become visible.

If the focus leaves the form (user clicks/taps/tabs outside any input), the inputs should hide.

I thought about using document.activeElement on the blur event of the form or the input fields to check where the focus was going after the input lost it. However, document.activeElement inside a blur event seems to always return body, not the element that is actually going to receive it.

This is what I got:

// whenever an input loses focus:
//   if the focus is still inside the form,
//      keep the inputs visible
//   if focus left the form
//      hide the inputs, leaving only the first visible
$('#new-journal input').blur(function (e) {
    var $active = $(document.activeElement);
    if ($active.closest('#new-journal').length === 0) {
        $('#new-journal input:gt(0)').addClass('hide');
    }
});

...considering that #new-journal is the ID of the form element.

// whenever the user clicks/focuses the "Add a journal" input, 
// the other inputs inside the form appear
$('#name').focus(function(e) {
  $('#new-journal input:gt(0)').removeClass('hide');
});

// whenever an input loses focus:
//   if the focus is still inside the form,
//      keep the inputs visible
//   if focus left the form
//      hide the inputs, leaving only the first visible
$('#new-journal input').blur(function(e) {
  var $active = $(document.activeElement);
  if ($active.closest('#new-journal').length === 0) {
    $('#new-journal input:gt(0)').addClass('hide');
  }
});


// just logs who is the element with focus (document.activeElement)
$('*').on('focus blur', function(e) {
  var $history = $('#history'),
    current = document.activeElement,
    label = current.tagName + ' ' + current.id;
  $history.html($history.html() + label + '\n');
});
input[type=text] {
  padding: 4px 6px;
  display: block;
  margin: 0 0 5px;
  box-sizing: border-box;
}
.hide {
  display: none !important;
  height: 0;
  margin: 0;
  padding: 0;
}
pre:not(:empty) {
  border-radius: 2px;
  background-color: #eee;
  padding: 5px;
}
.col {
  float: left;
  width: 45%;
  margin: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col">
  <form id="new-journal">
    <input type="text" id="name" placeholder="Add a journal" />
    <input type="text" id="field1" placeholder="Sub title" class="hide" />
    <input type="text" id="field2" placeholder="Owner" class="hide" />
    <input type="text" id="field3" placeholder="Price" class="hide" />
  </form>
</div>
<div class="col">
  <p>document.activeElement:</p>
  <pre id="history"></pre>
</div>

Any ideas on how to detect if the focus is still inside a form (any focusable container element) besides using document.activeElement?


Update: I also tried using CSS only, with these rules:

#new-journal input:not(#name) {
    display: none;
}
#new-journal input:focus ~ input {
    display: block !important;
}

...but I got the exact same result.

Upvotes: 0

Views: 1680

Answers (1)

Alvaro Montoro
Alvaro Montoro

Reputation: 29655

You can solve the problem by applying the focus function to all the inputs and not only to the first one (#name).

$('#new-journal input').focus(function (e) {
    $('#new-journal input:gt(0)').removeClass('hide');
});

That solves the problem and doesn't affect the initial layout. You can see it working here: http://jsfiddle.net/vyd5dje6/.

Upvotes: 1

Related Questions