html - Need to take click into account on this jquery -


this simple jquery shows text info when user press keys. works fine, except not not shows text "loading..." if user click on hyperlink.

how solve this, total noob in jquery.

jquery:

$(document).ready( function () {     $(document).keydown(         function(e) {           var url = false;           if (e.which == 37) { url = $('.pret a').attr('href'); }           else if (e.which == 39) { url = $('.sled a').attr('href'); }            if (url) { $('div.a37-sheet').hide().html('<center><p>loading...</p></center>').load(window.location = url).fadein(); }         });   }); 

html part:

<li class="pret button" style="left:0; top:50%; position:fixed"> <a class="kggkfx" style="outline: 0;" href="'.$pretlokacija.'"><img src="images/prev.png"></a> </li> <li class="sled button" style="right:0; top:50%; position:fixed"> <a class="kggkfx" style="outline: 0;" href="'.$sledlokacija.'"><img src="images/idigore.png"></a> </li> 

you didn't catch click event. why nothing works when user click hyperlink. add please:

$( '.kggkfx' ).click( function( event ){   event.preventdefault();   var = $( event.target ),       url = a.attr( 'href' ),       container = $( 'div.a37-sheet' ),       loader = '<center><p>loading...</p></center>';   container.hide().html( loader ).load( window.location = url ).fadein(); }); 

but code better when trying don't repeat yourself. have 2 pretty same event handlers. practice rewrite code to:

$(document).ready( function(){   $( document ).keydown( function( event ) {         if( e.which == 37 )           $('.pret a').click();         else if( e.which == 39 )           $('.sled a').click();   });    $( '.kggkfx' ).click( function( event ){       event.preventdefault();       var = $( event.target ),           url = a.attr( 'href' ),           container = $( 'div.a37-sheet' ),           loader = '<center><p>loading...</p></center>';       container.hide().html( loader ).load( window.location = url ).fadein();   }); }); 

Comments