how to use $ function on two events simultaniously in jquery -


i have text box , image button, want search content entered in text box (both enter on text box) , click on search image(icon) should work..

i tried :

$(document).ready(function () {       $('.customsearchbutton').on("click","img", function (e) {         var search = $("#txtsearch").val();                 var searchencoded = encodeuricomponent(search);         window.location = "/search.aspx?name=" + searchencoded;       }); }); 

my html :

 <li class="customtopnavsearch">                                             <input type="text" id="txtsearch" />                                             <a href="#" class="customsearchbutton">                                                 <img src="search-button.png" alt="search" />                                             </a>                                         </li> 

warp common code in function. bind function click event handler image , on click of enter call method.

function myeventhandler(){    var search = $("#txtsearch").val();             var searchencoded = encodeuricomponent(search);     window.location = "/search.aspx?name=" + searchencoded; }  $(document).ready(function () {      $('.customsearchbutton').on("click","img", myeventhandler);     $("#txtsearch").on("keyup",function (e) {         //if enter pressed call method         if(e.which == 13){             myeventhandler();         }      }); }); 

event.which

for key or mouse events, property indicates specific key or button pressed.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -