javascript - set focus to a div when any of it's content elements are clicked -


i use following set focus div when clicked or tabbed into.

it works when click padding area inside div.

clicking on of child elements not activate div:focus.

please how can fix this?

css

.sidebar-message:link { }  .sidebar-message:visited { }  .sidebar-message:hover {     background-color: lightyellow !important;     outline-width: 0 !important; }  .sidebar-message:focus {     background-color: khaki !important;     outline-width: 0 !important; } 

html

<div class="sidebar-message" tabindex="-1">      <a href="#" id="pending_{{id}}" data-id="{{id}}">      <div class="pull-left text-center">          <img alt="image" class="img-circle message-avatar" src="media/profile-pics/{{uid}}.jpg">      </div>      <div class="media-body">           <strong>{{{contact_name}}}</strong>           <span class="badge {{color}} pull-right{{hidden}}">{{unread}}</span>           <br>           <small class="text-muted">{{formatdate last_seen 'dddd, mmmm h:mm:ss a'}}</small>           <small class="pull-right">{{agent_name}}</small>      </div>      </a> </div> 

some notes:

1) there multiple .sidebar-message rows (divs), , 1 should highlighted @ time

2) list of .sidebar-message divs not available @ documentready, becomes available when xhr request returns data populated template via handlebars. 1 of comments suggested late-binding round this

this add new class container when of childs focus.

note: added class gets not removed in example once set. if want remove focused class if user clicks somewhere else, suggest implement handler focusout() accordingly.

another note: advise agains calling .focus() on container element (as suggested in 2 of other answers), steal focus child elements, in turn mess usability, if you're planing on adding input elements container.

$(document).ready(function() {    // use following line bind focus handler existing elements:    // $(".sidebar-message *").on('focus', function() {     // use following line late binding (dynamically created elements):    $(document).on("focus", ".sidebar-message *", function() {      // remove class .sidebar-message elements      $(".sidebar-message").removeclass("focused");      // add class nearest .sidebar-message element      $(this).closest(".sidebar-message").addclass("focused");    });  });
.sidebar-message:link {  }    .sidebar-message:visited {  }    .sidebar-message:hover {  background-color: lightyellow !important;  outline-width: 0 !important;  }    .focused.sidebar-message:hover,  .focused {  background-color: khaki !important;  outline-width: 0 !important;  }    .sidebar-message:focus {  background-color: khaki !important;  outline-width: 0 !important;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>  <div class="sidebar-message" tabindex="-1">    <a href="#" id="pending_{{id}}" data-id="{{id}}">      <div class="pull-left text-center">        <img alt="image" class="img-circle message-avatar" src="media/profile-pics/{{uid}}.jpg">      </div>      <div class="media-body">        <strong>{{{contact_name}}}</strong>        <span class="badge {{color}} pull-right{{hidden}}">{{unread}}</span>        <br>        <small class="text-muted">{{formatdate last_seen 'dddd, mmmm h:mm:ss a'}}</small>        <small class="pull-right">{{agent_name}}</small>      </div>    </a>  </div>    <hr>    <div class="sidebar-message" tabindex="-1">    <a href="#" id="pending_{{id}}" data-id="{{id}}">      <div class="pull-left text-center">        <img alt="image" class="img-circle message-avatar" src="media/profile-pics/{{uid}}.jpg">      </div>      <div class="media-body">        <strong>{{{contact_name}}}</strong>        <span class="badge {{color}} pull-right{{hidden}}">{{unread}}</span>        <br>        <small class="text-muted">{{formatdate last_seen 'dddd, mmmm h:mm:ss a'}}</small>        <small class="pull-right">{{agent_name}}</small>      </div>    </a>  </div>


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -