0

Is there any way using Javascript to send the "enter" aka keystroke "13" in an input when a button is clicked?

Details on why I want this:

Basically I have a car part look up tool that uses jQuery to actively post to a php file that searchs a MySQL DB, I wanted to make a few buttons with common car part OEMs, like; Chevrolet, Ford, Toyota etc.. I have the buttons doing they're thing when I click on them then change what is in the search field but it won't post to the php file until I press enter, so I wanted to hit two birds with one stone, click the button and it will enter 'chevrolet' in the search in put and then press enter for me all at once with one click of a button. =)

here is the code I am using to post data to the php file:

$(document).ready(function() {  

    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) {
        clearTimeout($.data(this, 'timer'));

        var search_string = $(this).val();

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

UPDATE:

I was able to make it work with the following code:

Javascript:

$(".btn3").click(function() { var e = $.Event('keyup'); e.which= 13; $('#search').val($(this).val()); $('#search').trigger(e); });

HTML:

<input type="button" class="btn3" id="CHEVROLET" value="CHEVROLET" />

b3ck
  • 97
  • 1
  • 3
  • 10
  • Can I ask why you wan't to do this? Or any example cases? – Rey Libutan Mar 28 '14 at 04:07
  • for what purpose? if it is to manually submit the form then it would be better to use `$('#FORMID').Submit();` – Jake Mar 28 '14 at 04:08
  • possible duplicate of [How to find the key code for a specific key](http://stackoverflow.com/questions/4054801/how-to-find-the-key-code-for-a-specific-key) – Saghir A. Khatri Mar 28 '14 at 04:10
  • basically I have a car part look up tool that uses jQuery to actively post to a php file that searchs a MySQL DB, I wanted to make a few buttons with common car part OEMs, like; Chevrolet, Ford, Toyota etc.. I have the buttons doing they're thing when I click on them then change what is in the search field but it won't post to the php file until I press enter, so I wanted to hit two birds with one stone, click the button and it will enter 'chevrolet' in the search in put and then press enter for me all at once with one click of a button.. that is why.. going to try out some code below.. – b3ck Mar 28 '14 at 04:26

2 Answers2

1

I'm assuming you have a function that handles the event of a user pressing the 'enter' key in the search input. Simply give that function a name and have it fire on the button click as well!

Alon Dahari
  • 1,138
  • 1
  • 9
  • 27
0

The way to do what you are asking:

$('#buttonID').click(function(){
// Create a new jQuery.Event object with 'keydown' event specified.
var e = jQuery.Event( "keydown", { keyCode: 13 } );

// trigger (send) an artificial keydown event with keyCode 13
jQuery( "body" ).trigger( e );
});

Depending on the application, if it is to make it so that the form submits when a non-submit button is pressed then

$('#buttonID').click(function(){
    $('#FormID').Submit();
});

would be the way to go.

Edit:

Alternatively to send the 'enter' command to the specific input box use:

$('#myInputId').trigger(jQuery.Event('keypress', {which: 13}));

You may need to replace which with keyCode depending on the version of jQuery you are using... JSFiddle Demo

from the updated description though, sounds like there must be a form on the page, if you can determine its id (look through source for <form .. id="FormID" then it would be better practice to use the submit function as above:

$('#buttonID').click(function(){
    $('#myInputID').val("Chevrolet"); // or however you are already doing this part
    $('#FormID').Submit();
});

What you should do is split the Javascript from the button so your final result will be something like:

    <script>

    $(".btn3").click(function() {
       $('#search').val($(this).val());
       $('#search').trigger(jQuery.Event('keypress', {which: 13}));
    });

    </script>
    <input type="button" class="btn3" id="Chevrolet" value="Chevrolet" />
<input type="button" class="btn3" id="Ford" value="Ford" />
<input type="button" class="btn3" id="Anotherone" value="Anotherone" />

What may be going wrong is you said you have multiple buttons, but keywords is a pretty generic id, which you may be repeating.. an ID must be unique or JS will not work correctly, if you want to perform a JS function for a group of things it is better to use the class, as this does not have to be unique.

so basically what the above does is waits for anything with class="btn3" in it to be clicked.

when that happens the input with id="search" gets filled with whatever the value= contains for what was clicked. Once that has happened the [enter] keypress is triggered inside the search input box.

This way, no matter which button gets pressed (Chevrolet, Ford or Anotherone) the search input box will get filled with the correct value. To add more buttons all you have to do is make sure they have class="btn3" and value="SOMETHING" and the above should work.

    $(document).ready(function() {  

        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) {
            clearTimeout($.data(this, 'timer'));

            var search_string = $(this).val();

            if (search_string == '') {
                $("ul#results").fadeOut();
                $('h4#results-text').fadeOut();
            }else{
                $("ul#results").fadeIn();
                $('h4#results-text').fadeIn();
                $(this).data('timer', setTimeout(search, 100));
            };
        });
    // ADD THIS
    $(".btn3").click(function() {
               $('#search').val($(this).val());
               search();
            });

    });

    ... 
<!-- Fix inputs -->
    <input type="button" class="btn3" id="Chevrolet" value="Chevrolet" />
        <input type="button" class="btn3" id="Ford" value="Ford" />
        <input type="button" class="btn3" id="Anotherone" value="Anotherone" />
Jake
  • 1,207
  • 2
  • 28
  • 46
  • I don't think this will work.. looks like it will just send the "enter" keystroke to the overall body after I click the button, I'll update my question to be more specific, thanks =) – b3ck Mar 28 '14 at 04:30
  • yea just tried this out.. not working the way I want it to =/ – b3ck Mar 28 '14 at 04:34
  • there is no form, jQuery just looks at the search input for updates and posts to the php file and shows the results as you are typing, but not when the buttons are pushed.. so with the new code you posted where would I put that exactly? I'm also using jQuery 1.7.2* – b3ck Mar 28 '14 at 04:47
  • You would put it in the button click event, or pretty much the line after where you are setting the input value to Chevrolet or whatever value is required for that button – Jake Mar 28 '14 at 04:51
  • Well right now I have it this way: `` – b3ck Mar 28 '14 at 04:56
  • yea I tried all of your suggestions.. still not working.. I am most likely not putting the code in the right place =/ – b3ck Mar 28 '14 at 05:10
  • JAKE!! I was able to to it with this code; ` $(".btn3").click(function() { var e = $.Event('keyup'); e.which= 13; $('#search').val($(this).val()); $('#search').trigger(e); });` – b3ck Mar 28 '14 at 15:03