Practical Web Programming

Thursday, June 20, 2013

Trigger Button Click on Enter Key in TextInput

Most of the time when you are in a search form, google search for example, after typing in your search phrase, you want to trigger the search by just pressing the Enter key in your keyboard. How annoying it would be if your have to reach for the mouse hit the search button with your cursor? Luckily, this is easily implemented in jQuery. 

Here's the full code example:
    <title>Search on Enter key</title>
    <script type="text/javascript">
      $(document).ready(function() {
        $("#search_text").on('keyup', function(event){
          if(event.keyCode == 13) {
        $("#search_button").on('click', function(e) {
          alert("You clicked me!");
      <input id="search_text" type="text" value=""/>
      <button id="search_button" href="#">Search</button>

Track across sub-domains in google analytics

Google analytics, tracks sub-domain separately. If you don't want that, add the following line in the tracking code:

_gaq.push(['_setDomainName', '']);

So your tracking code will look like this:

<script type="text/javascript">
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXX-X']);
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', '']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
(fugna.cstrico=n(()'h{ttps:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
var ga = document.createElement('script'); ga.type =
var s = document.getElementsByTagName('script')[0];
'text/javascript'; ga.async = true;
s.parentNode.insertBefore(ga, s);
ga.src = ('https:' == document.location.protocol ?
'https://ssl' : 'http://www') + '.google-';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

Recent Post