Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't hide ul after clicking li element

After clicking on input I open <ul> list, need to close it on clicking <li> or in another place of screen

Here My JS & HTML

$(".drop-down-input").click(function() {
  $(".dropdown-list").show();
});

// get "li" value and set to input

$(function() {
  $(".dropdown-list li").on('click', function() {
    $idinput = $(".dropdown-list").siblings('input').attr('id');
    $idinput = '#' + $idinput;
    $($idinput).val($(this).html());
    $(".dropdown-list").hide();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="styled-input-container drop-down-input">
  <input id="simple_2" class="drop-down-select" placeholder="input text" type="text">
  <ul class="dropdown-list">
    <li>eeee</li>
    <li>xxxx</li>
    <li>xxxx</li>
  </ul>
</div>

$(".dropdown-list").hide(); - this is not working, I don't now why?

like image 986
Егор Кротенко Avatar asked Jan 24 '26 11:01

Егор Кротенко


1 Answers

You need to bind the click event to the input element instead of the entire div otherwise event bubbling may happen. Although make it simple actually there is no need to get the id and select element again by the id value.

$(function() {
  $(".drop-down-input input").click(function() {
    $(".dropdown-list").show();
  });
  $(".dropdown-list li").on('click', function() {
    // if there is multiple `.dropdown-list` then get based on 
    // this context , eg : $(this).parent()
    $(".dropdown-list") 
      .hide() // hide the element
      .siblings('input') // get the sibling
      .val($(this).html()); // set it's value
  });
});

$(function() {
  $(".drop-down-input input").click(function() {
    $(".dropdown-list").show();
  });
  $(".dropdown-list li").on('click', function() {
    $(".dropdown-list")
      .hide()
      .siblings('input')
      .val($(this).html());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="styled-input-container drop-down-input">
  <input id="simple_2" class="drop-down-select" placeholder="input text" type="text">
  <ul class="dropdown-list">
    <li>eeee</li>
    <li>xxxx</li>
    <li>xxxx</li>
  </ul>
</div>

Or use event.stopPropagation() to prevent event bubbling up.

$(function() {
  $(".drop-down-input").click(function() {
    $(".dropdown-list").show();
  });
  $(".dropdown-list li").on('click', function(e) {
    e.stopPropagation();
    $(".dropdown-list")
      .hide()
      .siblings()
      .val($(this).html());
  });
});

$(function() {
  $(".drop-down-input").click(function() {
    $(".dropdown-list").show();
  });
  $(".dropdown-list li").on('click', function(e) {
    e.stopPropagation();
    $(".dropdown-list")
      .hide()
      .siblings()
      .val($(this).html());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="styled-input-container drop-down-input">
  <input id="simple_2" class="drop-down-select" placeholder="input text" type="text">
  <ul class="dropdown-list">
    <li>eeee</li>
    <li>xxxx</li>
    <li>xxxx</li>
  </ul>
</div>
like image 57
Pranav C Balan Avatar answered Jan 26 '26 23:01

Pranav C Balan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!