0

For those who can't wait, Fiddle :)

I am developing this kind of logic and I know I will be using jQuery for this but the problem is I don't know how to do such thing or to start it.

I want everytime I type something on the input or the input has text I want the .search-dropdown will be shown. Also even if I copy & paste something on the input the .search-dropdown will also be shown. How do I do it ? I don't really know how to do it in jQuery :( Forgive me for it I'm still a newbie in jQuery world.

For better example just like the search bar in facebook everytime I type the search results will be shown :). Thanks in advance for your time and your answers :) Have a good day Sir/Mam.

By is my CSS and HTML code:

CSS:

.search-here{
    width: 100%;
    height: 30px;
    border: 1px solid #000;
    padding: 5px 10px
}

.search-dropdown{
    width: 100%;
    height: auto;
    overflow: hidden;
    background: #ececec;
    border-radius: 2px;
    margin-top: 5px;
    display: none;
}

.search-dropdown a{
    display: block;
    padding: 5px;
    border-bottom: 1px solid #dddddd;
    text-decoration: none;
    color: #000;
}

HTML:

    <div class="box-a4">
      <form>
        <input type="text" placeholder="Search" class="search-here">
        <div class="search-dropdown">
                <a href="#">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</a>
                <a href="#">Dolorum a aperiam nemo ex fuga esse at expedita sed quo sequi voluptas labore.</a>
                <a href="#">Dolorum a aperiam nemo ex fuga esse at expedita sed quo sequi voluptas labore, nihil assumenda quidem dolore illo, consequuntur neque quasi. Lorem ipsum dolor sit amet, consectetur adipisicing elit.</a>
        </div>
      </form>
    </div>

Thank you :)

3
  • indeed I can't waith for jsfiddle, but there is no js code... Commented May 26, 2015 at 9:47
  • Yes there is no js code for I don't know how to do it :) Forgive me for that. Thanks Commented May 26, 2015 at 9:52
  • Use a datalist on HTML5 compliant browser Commented May 26, 2015 at 9:55

3 Answers 3

3

You can use:

$('input').on('input',function(){
  $('.search-dropdown').toggle(this.value.length > 0);
});

Working Demo

Sign up to request clarification or add additional context in comments.

8 Comments

the dropdown will be hidden every second character.
This works great but I want that the search results will be permanently be shown even if I type something on the input because as what I notice the search results will hide everytime I type :) Can you show to me how it will be done Sir ? :) Thanks. Anyway thank you for this
What about pasting some text? I would use input event
@A.Wolff: change event will handle that
@MilindAnantwar It would once input loses focus, not sure this is expected behaviour
|
1

You can use keyup and on functions. Demo

$( document ).ready(function() {
    $('.search-here').keyup(function() {
        if($('.search-here').val() == ""){
          $('.search-dropdown').hide();
        } else {
          $('.search-dropdown').show();
        }
    });    

    $('.search-here').on('paste', function () {
        $('.search-dropdown').show();
    }); 
});

1 Comment

This works but what if the input don't have text ? The search results don't hide :) But anyway thank you for your time sir.
0
$(".search-here").on("change",function(){
    //code to show auto-complete dropdown goes here
})
$(".search-here").on("keyup",function(){
    $(this).trigger("change");
})

The above code will allow you to do something anytime the text inside the textbox changes.

2 Comments

Explain wath you are doing with this code so that its clear to understand
change doesn't run on every keypress, it runs when they leave the input field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.