Refactoring duplicated jQuery script, Inputs focus .show() or .hide() div for multiple inputs and hidden divs












0












$begingroup$


I posted a question on StackOverflow and got an answer that works but has a lot of duplicated code. In the future, the functionality may also require a third or more divs to be shown or hidden.



The original script also came from a SO answer.



Can this code be refactored or improved to accommodate more than 2 inputs and hidden divs?



When the client focuses in a text field the associated div is shown or hidden on blur.






//BASED OFF SO SINGULAR EXAMPLE
//https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat#answer-2427363



//CAN THESE TWO BE REFACTORED???
$('#search-markets').focus(function() {
$('div.select-markets-filters').css('display', 'flex');
$(document).bind('focusin.select-markets-filters click.select-markets-filters',function(e) {
if ($(e.target).closest('.select-markets-filters, #search-markets').length) return;
$(document).unbind('.select-markets-filters');
$('div.select-markets-filters').slideUp(300);
});
});
$('div.select-markets-filters').hide();

//CAN THESE TWO BE REFACTORED???
$('#search-symbols-instruments').focus(function() {
$('div.select-symbols-instruments-filters').css('display', 'flex');
$(document).bind('focusin.select-symbols-instruments-filters click.select-symbols-instruments-filters',function(e) {
if ($(e.target).closest('.select-symbols-instruments-filters, #search-symbols-instruments').length) return;
$(document).unbind('.select-symbols-instruments-filters');
$('div.select-symbols-instruments-filters').slideUp(300);
});
});
$('div.select-symbols-instruments-filters').hide();

#select-data-inputs {
background-color: #000;
}

.select-filters {
background-color: rgba(0, 0, 0, 0.8);
border-top: 2px solid #fff;
color: #fff;
}

#select-symbols {
background-color: rgba(1, 56, 89, 0.85);
}

#select-markets {
background-color: rgba(2, 104, 165, 0.85);
}

.filter-list li.list-inline-item {
width: 48%;
margin: 0;
}

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">   

<div class="container-fluid">
<div class="row m-5 ">
<div class="col-12 text-center">
<h1>On form inputs focus, show div. hide div on blur for <span class="text-danger">multiple inputs</span> and hidden divs</h1>
<p class="lead"><i><a href="https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat" target="_blank">Based from SO sigular example</a></i></p>
</div>
</div>
<div class="row">
<div id="select-data-inputs" class="controls form-row p-3 w-100">
<div class="col-4">
<input type="text" id="search-markets" class="input form-control" placeholder="Search Markets">
</div>
<div class="col-4 offset-1">
<input type="text" id="search-symbols-instruments" class="input form-control" placeholder="Search Symbols">
</div>
</div>
</div>
<div id="main-display">
<div id="select-markets" class="row select-filters select-markets-filters p-4">
<div class="select-heading col-12 pl-2">
<h6 class="small-sub-heading">Select markets</h6>
</div>
<div class="col-4 pt-2 select-filter-items">
<ul class="filter-list list-unstyled pl-2">
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="market-option-1" value="market-option-1">
<label class="form-check-label" for="market-option-1">Market Option 1</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="market-option-2" value="market-option-2">
<label class="form-check-label" for="market-option-2">Market Option 2</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="market-option-3" value="market-option-3">
<label class="form-check-label" for="market-option-3">Market-Option 3</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="market-option-4" value="market-option-4">
<label class="form-check-label" for="market-option-4">Market-Option 4</label>
</li>
</ul>
</div>
</div>
<div id="select-symbols" class="row select-filters select-symbols-instruments-filters p-4">
<div class="select-heading col-4 offset-5 pl-2">
<h6 class="small-sub-heading">Select symbols</h6>
</div>
<div class="col-4 offset-5 pt-2 select-filter-items">
<ul class="filter-list list-unstyled pl-2">
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="symbol-option-1" value="symbol-option-1">
<label class="form-check-label" for="symbol-option-1">Symbol Option 1</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="symbol-option-2" value="symbol-option-2">
<label class="form-check-label" for="symbol-option-2">Symbol Option 2</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="symbol-option-3" value="symbol-option-3">
<label class="form-check-label" for="symbol-option-3">Symbol Option 3</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="symbol-option-4" value="symbol-option-4">
<label class="form-check-label" for="symbol-option-4">Symbol Option 4</label>
</li>
</ul>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js" integrity="sha384-pjaaA8dDz/5BgdFUPX6M/9SUZv4d12SUPF0axWc+VRZkx5xU3daN+lYb49+Ax+Tl" crossorigin="anonymous"></script>











share







New contributor




user191294 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    0












    $begingroup$


    I posted a question on StackOverflow and got an answer that works but has a lot of duplicated code. In the future, the functionality may also require a third or more divs to be shown or hidden.



    The original script also came from a SO answer.



    Can this code be refactored or improved to accommodate more than 2 inputs and hidden divs?



    When the client focuses in a text field the associated div is shown or hidden on blur.






    //BASED OFF SO SINGULAR EXAMPLE
    //https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat#answer-2427363



    //CAN THESE TWO BE REFACTORED???
    $('#search-markets').focus(function() {
    $('div.select-markets-filters').css('display', 'flex');
    $(document).bind('focusin.select-markets-filters click.select-markets-filters',function(e) {
    if ($(e.target).closest('.select-markets-filters, #search-markets').length) return;
    $(document).unbind('.select-markets-filters');
    $('div.select-markets-filters').slideUp(300);
    });
    });
    $('div.select-markets-filters').hide();

    //CAN THESE TWO BE REFACTORED???
    $('#search-symbols-instruments').focus(function() {
    $('div.select-symbols-instruments-filters').css('display', 'flex');
    $(document).bind('focusin.select-symbols-instruments-filters click.select-symbols-instruments-filters',function(e) {
    if ($(e.target).closest('.select-symbols-instruments-filters, #search-symbols-instruments').length) return;
    $(document).unbind('.select-symbols-instruments-filters');
    $('div.select-symbols-instruments-filters').slideUp(300);
    });
    });
    $('div.select-symbols-instruments-filters').hide();

    #select-data-inputs {
    background-color: #000;
    }

    .select-filters {
    background-color: rgba(0, 0, 0, 0.8);
    border-top: 2px solid #fff;
    color: #fff;
    }

    #select-symbols {
    background-color: rgba(1, 56, 89, 0.85);
    }

    #select-markets {
    background-color: rgba(2, 104, 165, 0.85);
    }

    .filter-list li.list-inline-item {
    width: 48%;
    margin: 0;
    }

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">   

    <div class="container-fluid">
    <div class="row m-5 ">
    <div class="col-12 text-center">
    <h1>On form inputs focus, show div. hide div on blur for <span class="text-danger">multiple inputs</span> and hidden divs</h1>
    <p class="lead"><i><a href="https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat" target="_blank">Based from SO sigular example</a></i></p>
    </div>
    </div>
    <div class="row">
    <div id="select-data-inputs" class="controls form-row p-3 w-100">
    <div class="col-4">
    <input type="text" id="search-markets" class="input form-control" placeholder="Search Markets">
    </div>
    <div class="col-4 offset-1">
    <input type="text" id="search-symbols-instruments" class="input form-control" placeholder="Search Symbols">
    </div>
    </div>
    </div>
    <div id="main-display">
    <div id="select-markets" class="row select-filters select-markets-filters p-4">
    <div class="select-heading col-12 pl-2">
    <h6 class="small-sub-heading">Select markets</h6>
    </div>
    <div class="col-4 pt-2 select-filter-items">
    <ul class="filter-list list-unstyled pl-2">
    <li class="list-inline-item">
    <input class="form-check-input" type="checkbox" id="market-option-1" value="market-option-1">
    <label class="form-check-label" for="market-option-1">Market Option 1</label>
    </li>
    <li class="list-inline-item">
    <input class="form-check-input" type="checkbox" id="market-option-2" value="market-option-2">
    <label class="form-check-label" for="market-option-2">Market Option 2</label>
    </li>
    <li class="list-inline-item">
    <input class="form-check-input" type="checkbox" id="market-option-3" value="market-option-3">
    <label class="form-check-label" for="market-option-3">Market-Option 3</label>
    </li>
    <li class="list-inline-item">
    <input class="form-check-input" type="checkbox" id="market-option-4" value="market-option-4">
    <label class="form-check-label" for="market-option-4">Market-Option 4</label>
    </li>
    </ul>
    </div>
    </div>
    <div id="select-symbols" class="row select-filters select-symbols-instruments-filters p-4">
    <div class="select-heading col-4 offset-5 pl-2">
    <h6 class="small-sub-heading">Select symbols</h6>
    </div>
    <div class="col-4 offset-5 pt-2 select-filter-items">
    <ul class="filter-list list-unstyled pl-2">
    <li class="list-inline-item">
    <input class="form-check-input" type="checkbox" id="symbol-option-1" value="symbol-option-1">
    <label class="form-check-label" for="symbol-option-1">Symbol Option 1</label>
    </li>
    <li class="list-inline-item">
    <input class="form-check-input" type="checkbox" id="symbol-option-2" value="symbol-option-2">
    <label class="form-check-label" for="symbol-option-2">Symbol Option 2</label>
    </li>
    <li class="list-inline-item">
    <input class="form-check-input" type="checkbox" id="symbol-option-3" value="symbol-option-3">
    <label class="form-check-label" for="symbol-option-3">Symbol Option 3</label>
    </li>
    <li class="list-inline-item">
    <input class="form-check-input" type="checkbox" id="symbol-option-4" value="symbol-option-4">
    <label class="form-check-label" for="symbol-option-4">Symbol Option 4</label>
    </li>
    </ul>
    </div>
    </div>
    </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js" integrity="sha384-pjaaA8dDz/5BgdFUPX6M/9SUZv4d12SUPF0axWc+VRZkx5xU3daN+lYb49+Ax+Tl" crossorigin="anonymous"></script>











    share







    New contributor




    user191294 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      0












      0








      0





      $begingroup$


      I posted a question on StackOverflow and got an answer that works but has a lot of duplicated code. In the future, the functionality may also require a third or more divs to be shown or hidden.



      The original script also came from a SO answer.



      Can this code be refactored or improved to accommodate more than 2 inputs and hidden divs?



      When the client focuses in a text field the associated div is shown or hidden on blur.






      //BASED OFF SO SINGULAR EXAMPLE
      //https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat#answer-2427363



      //CAN THESE TWO BE REFACTORED???
      $('#search-markets').focus(function() {
      $('div.select-markets-filters').css('display', 'flex');
      $(document).bind('focusin.select-markets-filters click.select-markets-filters',function(e) {
      if ($(e.target).closest('.select-markets-filters, #search-markets').length) return;
      $(document).unbind('.select-markets-filters');
      $('div.select-markets-filters').slideUp(300);
      });
      });
      $('div.select-markets-filters').hide();

      //CAN THESE TWO BE REFACTORED???
      $('#search-symbols-instruments').focus(function() {
      $('div.select-symbols-instruments-filters').css('display', 'flex');
      $(document).bind('focusin.select-symbols-instruments-filters click.select-symbols-instruments-filters',function(e) {
      if ($(e.target).closest('.select-symbols-instruments-filters, #search-symbols-instruments').length) return;
      $(document).unbind('.select-symbols-instruments-filters');
      $('div.select-symbols-instruments-filters').slideUp(300);
      });
      });
      $('div.select-symbols-instruments-filters').hide();

      #select-data-inputs {
      background-color: #000;
      }

      .select-filters {
      background-color: rgba(0, 0, 0, 0.8);
      border-top: 2px solid #fff;
      color: #fff;
      }

      #select-symbols {
      background-color: rgba(1, 56, 89, 0.85);
      }

      #select-markets {
      background-color: rgba(2, 104, 165, 0.85);
      }

      .filter-list li.list-inline-item {
      width: 48%;
      margin: 0;
      }

      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">   

      <div class="container-fluid">
      <div class="row m-5 ">
      <div class="col-12 text-center">
      <h1>On form inputs focus, show div. hide div on blur for <span class="text-danger">multiple inputs</span> and hidden divs</h1>
      <p class="lead"><i><a href="https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat" target="_blank">Based from SO sigular example</a></i></p>
      </div>
      </div>
      <div class="row">
      <div id="select-data-inputs" class="controls form-row p-3 w-100">
      <div class="col-4">
      <input type="text" id="search-markets" class="input form-control" placeholder="Search Markets">
      </div>
      <div class="col-4 offset-1">
      <input type="text" id="search-symbols-instruments" class="input form-control" placeholder="Search Symbols">
      </div>
      </div>
      </div>
      <div id="main-display">
      <div id="select-markets" class="row select-filters select-markets-filters p-4">
      <div class="select-heading col-12 pl-2">
      <h6 class="small-sub-heading">Select markets</h6>
      </div>
      <div class="col-4 pt-2 select-filter-items">
      <ul class="filter-list list-unstyled pl-2">
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-1" value="market-option-1">
      <label class="form-check-label" for="market-option-1">Market Option 1</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-2" value="market-option-2">
      <label class="form-check-label" for="market-option-2">Market Option 2</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-3" value="market-option-3">
      <label class="form-check-label" for="market-option-3">Market-Option 3</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-4" value="market-option-4">
      <label class="form-check-label" for="market-option-4">Market-Option 4</label>
      </li>
      </ul>
      </div>
      </div>
      <div id="select-symbols" class="row select-filters select-symbols-instruments-filters p-4">
      <div class="select-heading col-4 offset-5 pl-2">
      <h6 class="small-sub-heading">Select symbols</h6>
      </div>
      <div class="col-4 offset-5 pt-2 select-filter-items">
      <ul class="filter-list list-unstyled pl-2">
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-1" value="symbol-option-1">
      <label class="form-check-label" for="symbol-option-1">Symbol Option 1</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-2" value="symbol-option-2">
      <label class="form-check-label" for="symbol-option-2">Symbol Option 2</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-3" value="symbol-option-3">
      <label class="form-check-label" for="symbol-option-3">Symbol Option 3</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-4" value="symbol-option-4">
      <label class="form-check-label" for="symbol-option-4">Symbol Option 4</label>
      </li>
      </ul>
      </div>
      </div>
      </div>
      </div>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js" integrity="sha384-pjaaA8dDz/5BgdFUPX6M/9SUZv4d12SUPF0axWc+VRZkx5xU3daN+lYb49+Ax+Tl" crossorigin="anonymous"></script>











      share







      New contributor




      user191294 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I posted a question on StackOverflow and got an answer that works but has a lot of duplicated code. In the future, the functionality may also require a third or more divs to be shown or hidden.



      The original script also came from a SO answer.



      Can this code be refactored or improved to accommodate more than 2 inputs and hidden divs?



      When the client focuses in a text field the associated div is shown or hidden on blur.






      //BASED OFF SO SINGULAR EXAMPLE
      //https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat#answer-2427363



      //CAN THESE TWO BE REFACTORED???
      $('#search-markets').focus(function() {
      $('div.select-markets-filters').css('display', 'flex');
      $(document).bind('focusin.select-markets-filters click.select-markets-filters',function(e) {
      if ($(e.target).closest('.select-markets-filters, #search-markets').length) return;
      $(document).unbind('.select-markets-filters');
      $('div.select-markets-filters').slideUp(300);
      });
      });
      $('div.select-markets-filters').hide();

      //CAN THESE TWO BE REFACTORED???
      $('#search-symbols-instruments').focus(function() {
      $('div.select-symbols-instruments-filters').css('display', 'flex');
      $(document).bind('focusin.select-symbols-instruments-filters click.select-symbols-instruments-filters',function(e) {
      if ($(e.target).closest('.select-symbols-instruments-filters, #search-symbols-instruments').length) return;
      $(document).unbind('.select-symbols-instruments-filters');
      $('div.select-symbols-instruments-filters').slideUp(300);
      });
      });
      $('div.select-symbols-instruments-filters').hide();

      #select-data-inputs {
      background-color: #000;
      }

      .select-filters {
      background-color: rgba(0, 0, 0, 0.8);
      border-top: 2px solid #fff;
      color: #fff;
      }

      #select-symbols {
      background-color: rgba(1, 56, 89, 0.85);
      }

      #select-markets {
      background-color: rgba(2, 104, 165, 0.85);
      }

      .filter-list li.list-inline-item {
      width: 48%;
      margin: 0;
      }

      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">   

      <div class="container-fluid">
      <div class="row m-5 ">
      <div class="col-12 text-center">
      <h1>On form inputs focus, show div. hide div on blur for <span class="text-danger">multiple inputs</span> and hidden divs</h1>
      <p class="lead"><i><a href="https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat" target="_blank">Based from SO sigular example</a></i></p>
      </div>
      </div>
      <div class="row">
      <div id="select-data-inputs" class="controls form-row p-3 w-100">
      <div class="col-4">
      <input type="text" id="search-markets" class="input form-control" placeholder="Search Markets">
      </div>
      <div class="col-4 offset-1">
      <input type="text" id="search-symbols-instruments" class="input form-control" placeholder="Search Symbols">
      </div>
      </div>
      </div>
      <div id="main-display">
      <div id="select-markets" class="row select-filters select-markets-filters p-4">
      <div class="select-heading col-12 pl-2">
      <h6 class="small-sub-heading">Select markets</h6>
      </div>
      <div class="col-4 pt-2 select-filter-items">
      <ul class="filter-list list-unstyled pl-2">
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-1" value="market-option-1">
      <label class="form-check-label" for="market-option-1">Market Option 1</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-2" value="market-option-2">
      <label class="form-check-label" for="market-option-2">Market Option 2</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-3" value="market-option-3">
      <label class="form-check-label" for="market-option-3">Market-Option 3</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-4" value="market-option-4">
      <label class="form-check-label" for="market-option-4">Market-Option 4</label>
      </li>
      </ul>
      </div>
      </div>
      <div id="select-symbols" class="row select-filters select-symbols-instruments-filters p-4">
      <div class="select-heading col-4 offset-5 pl-2">
      <h6 class="small-sub-heading">Select symbols</h6>
      </div>
      <div class="col-4 offset-5 pt-2 select-filter-items">
      <ul class="filter-list list-unstyled pl-2">
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-1" value="symbol-option-1">
      <label class="form-check-label" for="symbol-option-1">Symbol Option 1</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-2" value="symbol-option-2">
      <label class="form-check-label" for="symbol-option-2">Symbol Option 2</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-3" value="symbol-option-3">
      <label class="form-check-label" for="symbol-option-3">Symbol Option 3</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-4" value="symbol-option-4">
      <label class="form-check-label" for="symbol-option-4">Symbol Option 4</label>
      </li>
      </ul>
      </div>
      </div>
      </div>
      </div>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js" integrity="sha384-pjaaA8dDz/5BgdFUPX6M/9SUZv4d12SUPF0axWc+VRZkx5xU3daN+lYb49+Ax+Tl" crossorigin="anonymous"></script>








      //BASED OFF SO SINGULAR EXAMPLE
      //https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat#answer-2427363



      //CAN THESE TWO BE REFACTORED???
      $('#search-markets').focus(function() {
      $('div.select-markets-filters').css('display', 'flex');
      $(document).bind('focusin.select-markets-filters click.select-markets-filters',function(e) {
      if ($(e.target).closest('.select-markets-filters, #search-markets').length) return;
      $(document).unbind('.select-markets-filters');
      $('div.select-markets-filters').slideUp(300);
      });
      });
      $('div.select-markets-filters').hide();

      //CAN THESE TWO BE REFACTORED???
      $('#search-symbols-instruments').focus(function() {
      $('div.select-symbols-instruments-filters').css('display', 'flex');
      $(document).bind('focusin.select-symbols-instruments-filters click.select-symbols-instruments-filters',function(e) {
      if ($(e.target).closest('.select-symbols-instruments-filters, #search-symbols-instruments').length) return;
      $(document).unbind('.select-symbols-instruments-filters');
      $('div.select-symbols-instruments-filters').slideUp(300);
      });
      });
      $('div.select-symbols-instruments-filters').hide();

      #select-data-inputs {
      background-color: #000;
      }

      .select-filters {
      background-color: rgba(0, 0, 0, 0.8);
      border-top: 2px solid #fff;
      color: #fff;
      }

      #select-symbols {
      background-color: rgba(1, 56, 89, 0.85);
      }

      #select-markets {
      background-color: rgba(2, 104, 165, 0.85);
      }

      .filter-list li.list-inline-item {
      width: 48%;
      margin: 0;
      }

      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">   

      <div class="container-fluid">
      <div class="row m-5 ">
      <div class="col-12 text-center">
      <h1>On form inputs focus, show div. hide div on blur for <span class="text-danger">multiple inputs</span> and hidden divs</h1>
      <p class="lead"><i><a href="https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat" target="_blank">Based from SO sigular example</a></i></p>
      </div>
      </div>
      <div class="row">
      <div id="select-data-inputs" class="controls form-row p-3 w-100">
      <div class="col-4">
      <input type="text" id="search-markets" class="input form-control" placeholder="Search Markets">
      </div>
      <div class="col-4 offset-1">
      <input type="text" id="search-symbols-instruments" class="input form-control" placeholder="Search Symbols">
      </div>
      </div>
      </div>
      <div id="main-display">
      <div id="select-markets" class="row select-filters select-markets-filters p-4">
      <div class="select-heading col-12 pl-2">
      <h6 class="small-sub-heading">Select markets</h6>
      </div>
      <div class="col-4 pt-2 select-filter-items">
      <ul class="filter-list list-unstyled pl-2">
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-1" value="market-option-1">
      <label class="form-check-label" for="market-option-1">Market Option 1</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-2" value="market-option-2">
      <label class="form-check-label" for="market-option-2">Market Option 2</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-3" value="market-option-3">
      <label class="form-check-label" for="market-option-3">Market-Option 3</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-4" value="market-option-4">
      <label class="form-check-label" for="market-option-4">Market-Option 4</label>
      </li>
      </ul>
      </div>
      </div>
      <div id="select-symbols" class="row select-filters select-symbols-instruments-filters p-4">
      <div class="select-heading col-4 offset-5 pl-2">
      <h6 class="small-sub-heading">Select symbols</h6>
      </div>
      <div class="col-4 offset-5 pt-2 select-filter-items">
      <ul class="filter-list list-unstyled pl-2">
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-1" value="symbol-option-1">
      <label class="form-check-label" for="symbol-option-1">Symbol Option 1</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-2" value="symbol-option-2">
      <label class="form-check-label" for="symbol-option-2">Symbol Option 2</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-3" value="symbol-option-3">
      <label class="form-check-label" for="symbol-option-3">Symbol Option 3</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-4" value="symbol-option-4">
      <label class="form-check-label" for="symbol-option-4">Symbol Option 4</label>
      </li>
      </ul>
      </div>
      </div>
      </div>
      </div>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js" integrity="sha384-pjaaA8dDz/5BgdFUPX6M/9SUZv4d12SUPF0axWc+VRZkx5xU3daN+lYb49+Ax+Tl" crossorigin="anonymous"></script>





      //BASED OFF SO SINGULAR EXAMPLE
      //https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat#answer-2427363



      //CAN THESE TWO BE REFACTORED???
      $('#search-markets').focus(function() {
      $('div.select-markets-filters').css('display', 'flex');
      $(document).bind('focusin.select-markets-filters click.select-markets-filters',function(e) {
      if ($(e.target).closest('.select-markets-filters, #search-markets').length) return;
      $(document).unbind('.select-markets-filters');
      $('div.select-markets-filters').slideUp(300);
      });
      });
      $('div.select-markets-filters').hide();

      //CAN THESE TWO BE REFACTORED???
      $('#search-symbols-instruments').focus(function() {
      $('div.select-symbols-instruments-filters').css('display', 'flex');
      $(document).bind('focusin.select-symbols-instruments-filters click.select-symbols-instruments-filters',function(e) {
      if ($(e.target).closest('.select-symbols-instruments-filters, #search-symbols-instruments').length) return;
      $(document).unbind('.select-symbols-instruments-filters');
      $('div.select-symbols-instruments-filters').slideUp(300);
      });
      });
      $('div.select-symbols-instruments-filters').hide();

      #select-data-inputs {
      background-color: #000;
      }

      .select-filters {
      background-color: rgba(0, 0, 0, 0.8);
      border-top: 2px solid #fff;
      color: #fff;
      }

      #select-symbols {
      background-color: rgba(1, 56, 89, 0.85);
      }

      #select-markets {
      background-color: rgba(2, 104, 165, 0.85);
      }

      .filter-list li.list-inline-item {
      width: 48%;
      margin: 0;
      }

      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">   

      <div class="container-fluid">
      <div class="row m-5 ">
      <div class="col-12 text-center">
      <h1>On form inputs focus, show div. hide div on blur for <span class="text-danger">multiple inputs</span> and hidden divs</h1>
      <p class="lead"><i><a href="https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat" target="_blank">Based from SO sigular example</a></i></p>
      </div>
      </div>
      <div class="row">
      <div id="select-data-inputs" class="controls form-row p-3 w-100">
      <div class="col-4">
      <input type="text" id="search-markets" class="input form-control" placeholder="Search Markets">
      </div>
      <div class="col-4 offset-1">
      <input type="text" id="search-symbols-instruments" class="input form-control" placeholder="Search Symbols">
      </div>
      </div>
      </div>
      <div id="main-display">
      <div id="select-markets" class="row select-filters select-markets-filters p-4">
      <div class="select-heading col-12 pl-2">
      <h6 class="small-sub-heading">Select markets</h6>
      </div>
      <div class="col-4 pt-2 select-filter-items">
      <ul class="filter-list list-unstyled pl-2">
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-1" value="market-option-1">
      <label class="form-check-label" for="market-option-1">Market Option 1</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-2" value="market-option-2">
      <label class="form-check-label" for="market-option-2">Market Option 2</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-3" value="market-option-3">
      <label class="form-check-label" for="market-option-3">Market-Option 3</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="market-option-4" value="market-option-4">
      <label class="form-check-label" for="market-option-4">Market-Option 4</label>
      </li>
      </ul>
      </div>
      </div>
      <div id="select-symbols" class="row select-filters select-symbols-instruments-filters p-4">
      <div class="select-heading col-4 offset-5 pl-2">
      <h6 class="small-sub-heading">Select symbols</h6>
      </div>
      <div class="col-4 offset-5 pt-2 select-filter-items">
      <ul class="filter-list list-unstyled pl-2">
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-1" value="symbol-option-1">
      <label class="form-check-label" for="symbol-option-1">Symbol Option 1</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-2" value="symbol-option-2">
      <label class="form-check-label" for="symbol-option-2">Symbol Option 2</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-3" value="symbol-option-3">
      <label class="form-check-label" for="symbol-option-3">Symbol Option 3</label>
      </li>
      <li class="list-inline-item">
      <input class="form-check-input" type="checkbox" id="symbol-option-4" value="symbol-option-4">
      <label class="form-check-label" for="symbol-option-4">Symbol Option 4</label>
      </li>
      </ul>
      </div>
      </div>
      </div>
      </div>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js" integrity="sha384-pjaaA8dDz/5BgdFUPX6M/9SUZv4d12SUPF0axWc+VRZkx5xU3daN+lYb49+Ax+Tl" crossorigin="anonymous"></script>






      performance jquery html form





      share







      New contributor




      user191294 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      user191294 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      user191294 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 7 mins ago









      user191294user191294

      1




      1




      New contributor




      user191294 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user191294 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user191294 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          user191294 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212253%2frefactoring-duplicated-jquery-script-inputs-focus-show-or-hide-div-for-mu%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          user191294 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          user191294 is a new contributor. Be nice, and check out our Code of Conduct.













          user191294 is a new contributor. Be nice, and check out our Code of Conduct.












          user191294 is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212253%2frefactoring-duplicated-jquery-script-inputs-focus-show-or-hide-div-for-mu%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga