$("#formaddnews").click(function(){ $("#formaddnewsmenu ").animate({opacity: "show"}, 500); if ($("#formaddnewsmenu").blur && $("#formaddnews").blur) {$("#formaddnewsmenu").animate({opacity: "hide"}, 500);}; }); 

Help with the focus!

How do I make sure that when the focus is lost by two objects, one of them hides

HTML code

 <form> <input id="formaddnews" class="formaddnews" placeholder="Добавить запись на стене" type="text"> <form> </center> </div> <div id="formaddnewsmenu" style=""> <img src="images/camera_7603.png"> <img src="images/forward_1087.png"> <img src="images/volume2_2290.png"> <div align="center" id="form_add_news_menu_bottom">Опубликовать</div> 

Js script

  <script> $("#formaddnews").click(function(){ $("#formaddnewsmenu ").animate({opacity: "show"}, 500); }); $("#formaddnewsmenu").focusout(function() { $("#formaddnewsmenu ").animate({opacity: "hide"}, 500); }) </script> 

    1 answer 1

    On jQuery , using .focusout() you can track the loss of focus.

     $("#formaddnewsmenu").focusout(function() { $("#formaddnewsmenu ").animate({opacity: "hide"}, 500); }) 
     .inputs { float: left; margin-right: 1em; } .inputs p { margin-top: 0; } 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="inputs"> <p> <input id="formaddnewsmenu" type="text"> </p> </div> <div id="focus-count"></div> 

    • $ ("# Formaddnewsmenu"). Focusout (function () {$ ("# formaddnewsmenu") .animate ({opacity: "hide"}, 500);}) does not work - TomasRedl
    • @TomasRedl, fixed the answer, works - Abmin
    • Damn does not work, maybe because I do not have a form but a DIV tag of the second object. It would be necessary for the DIV tag to work - TomasRedl
    • Added another code - TomasRedl
    • @TomasRedl, by default, the div cannot be in focus - Abmin