0

I tried every solution out there for this problem including.

I'm trying to copy the value from a div to an search input and, then, press enter. The search input it's from list.js and automatically show results when you type but copying the values doesn't have the same effect.

Here's my code:

<input type="search" class="search" placeholder="Pesquisar..." id="pesq" />

<div id="cat">
 <div class="test">Premium</div>
 <div class="test">Freemium</div>
 <div class="test">Free</div>
</div>

<script>
$(document).ready(function(){
 $("#cat div").click(function(){
  var value = $(this).html();
  var input = $('#pesq');
  var e = $.Event( "keypress", { which: 13 } );
  input.focus().val(value).trigger(e);
 });
});
</script>

Help, please!

Cypriano
  • 21
  • 6
  • You actually don't need to trigger "ENTER" to accomplish this. You just need to submit the form using the clicked value. First assign the value to the search input and then submit the form. – Sergio Oct 04 '22 at 21:23

1 Answers1

0

why not use the .search() API

var input = $('#pesq');
$("#cat div").click(function() {
  var value = $(this).html();
  input.val(value)
  catList.search(value)
});
var options = {valueNames: ['cats']};
var catList = new List('membership', options);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/list.js/2.3.1/list.min.js"></script>

<div id="cat">
    <div class="test">Premium</div>
    <div class="test">Freemium</div>
    <div class="test">Free</div>
</div>

<div id="membership">
    <input id="pesq" class="search" placeholder="Search" />
    <ul class="list">
        <li><div class="cats">Premium</div></li>
        <li><div class="cats">Freemium</div></li>
        <li><div class="cats">Free</div></li>
    </ul>
</div>
uingtea
  • 6,002
  • 2
  • 26
  • 40
  • Thanks, but list.js just allow one filter at the time, search with input is my "simple" solution to use two (the filter and search simultaneous)... i founded too an solution with split() and using something called "Lodash" but i'm having so much trouble with it because i'm too newbie :p – Cypriano Oct 04 '22 at 19:20
  • I don't understand, because in your question there are no `filter` – uingtea Oct 04 '22 at 23:10