0

I am working on a text field which is pressable with "enter" button but unfortunately it does not work. I am trying following code inside my project.

This is my JavaScript code:

<script>
$(document).ready(function() {  
    // Live Search
    // On Search Submit and Get Results
    function search() {
        var query_value = $('input#search').val();
        $('b#search-string').html(query_value);
        if(query_value !== ''){
            $.ajax({
                type: "POST",
                url: "search.php",
                data: { query: query_value },
                cache: false,
                success: function(html){
                    $("ul#results").html(html);
                }
            });
        }
            return false;    
    }

    $("input#search").live("keyup", function(e) {
        var theSearch = $('#search');
        // Set Timeout
        clearTimeout($.data(this, 'timer'));

        // Set Search String
        var search_string = $(this).val();

        // Do Search
        if (search_string == '') {
            $("ul#results").fadeOut();
            $('h4#results-text').fadeOut();
        }else{
            $("ul#results").fadeIn();
            $('h4#results-text').fadeIn();
            $(this).data('timer', setTimeout(search, 0));
        };
    });

});

    $("#search").on('keyup',function(e){
    var text = document.getElementById("search").value;
    if(text !== ""){
        if (e.which == 13){
            $("#btnSearch").click();
        }
    }   
});

function doSomething(){
    var text = document.getElementById("search").value;
    window.location.assign("search.php?value = " + text);
}
});

</script>

This is my HTML code:

<input type="text" id="search">
<input type="button" id="btnSearch" onclick="doSomething();" />
6
  • is your textbox comes in ajax result? Commented May 27, 2014 at 4:32
  • Do you have your javascript tag before or after the HTML? Commented May 27, 2014 at 4:32
  • What you means javascript tag? Commented May 27, 2014 at 4:33
  • <script> tags....because it seems to be working as expected jsfiddle.net/LPnDD Commented May 27, 2014 at 4:33
  • @RashminJaviya : I am doing search function inside my code and its work well. I just wanna to addin additional function when I key in value inside in my text field and it will redirect me to a new page which will show relevant result for what I am key in inside my text field. Commented May 27, 2014 at 4:35

3 Answers 3

2

use unique selector id

$("#search").on('keyup',function(e){

    if (e.which == 13){
        alert('abc');
    }
});

demo jsfiddle

use input type selector

$('input[type=text]').each(function(i,e) {

    $("#"+e.id).on('keyup',function(e){

        if (e.which == 13){
            alert('abc');
        }
    });
});

another demo

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

5 Comments

its work well at jsfiddle but when I dump your code inside my project it cannot work.
when i press on enter button it did not prompt out alert box/
do you remove your original $("input[type=text]").on('keyup',function(event){ code ?
yap. Actually I used your code before I post on here and its also cannot work.
remove your .live and try my solution to diagnosis the problem.
1

Place $("input[type=text]").on... inside $(document).ready and make sure you are using JQuery version before 1.7 If you wish to use live

4 Comments

I think your answer is the solution
Verified. the problem came from .live
I have updated my code where it cannot work when I replace "alert('abc')" to "$('#btnSearch').click()". click function cannot work. Hope you can give me another solution.
@user3517652, Place doSomething function before document.ready
0

change your html to

  <form onsubmit="doSomething();" >
   <input type="text" id="search">
   <input type="submit" id="btnSearch"  />
  </form>

while using this you don't have to listen enter key event your doSomething() function will be called automatically on enter press in input field

DEMO

Comments

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.