Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap input field call function on return keypress if field is focused

I have an input group in bootstrap which is an icon, a field and a button - for the purposes of user interface experience I would like the return key to call the function attached to the search button but only if that field is in focus.

Is this possible with native bootstrap or will some form of jQuery be needed? I wouldn't want the return keypress to be permanently used to activate this function, only if the user is using this input search field.

Thanks in advance

<div class="form-group">
  <label class="control-label" for="another-location">Search for another location</label>

  <div class="input-group">
    <div class="input-group-addon" data-toggle="tooltip" data-placement="right" title="Location"><span class="fa fa-map-marker fa-fw"></span></div>
    <input type="text" class="form-control gllpSearchField" id="another-location" placeholder="A location" name="another-location" />

    <span class="input-group-btn">
      <button class="btn btn-default" type="button">Search</button>
    </span>                             
  </div>
</div>
like image 988
user3779703 Avatar asked May 06 '15 09:05

user3779703


Video Answer


1 Answers

Yes you need Jquery :

$('#another-location').keypress(function(event){
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if(keycode == '13'){
           alert('You pressed a "enter" key in textbox, here submit your form'); 
        }
    });
like image 157
Raymond Avatar answered Sep 19 '22 14:09

Raymond